本文整理汇总了C#中System.Threading.Barrier.AddParticipants方法的典型用法代码示例。如果您正苦于以下问题:C# Barrier.AddParticipants方法的具体用法?C# Barrier.AddParticipants怎么用?C# Barrier.AddParticipants使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Threading.Barrier
的用法示例。
在下文中一共展示了Barrier.AddParticipants方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RunBarrierTest4_AddParticipants
/// <summary>
/// Test AddParticipants
/// </summary>
/// <param name="initialCount">The initial barrier participants count</param>
/// <param name="participantsToAdd">The aprticipants that will be added</param>
/// <param name="exceptionType">Type of the exception in case of invalid input, null for valid cases</param>
/// <returns>Tru if the test succeeded, false otherwise</returns>
private static bool RunBarrierTest4_AddParticipants(int initialCount, int participantsToAdd, Type exceptionType)
{
TestHarness.TestLog("AddParticipants(" + initialCount + "," + participantsToAdd + ")");
Barrier b = new Barrier(initialCount);
Exception exception = null;
try
{
if (b.AddParticipants(participantsToAdd) != 0)
{
TestHarness.TestLog("AddParticipants failed, the return phase count is not correct");
return false;
}
if (b.ParticipantCount != initialCount + participantsToAdd)
{
TestHarness.TestLog("AddParticipants failed, total participant was not increased");
return false;
}
}
catch (Exception ex)
{
exception = ex;
}
if (exception != null && exceptionType == null)
{
TestHarness.TestLog("AddParticipants failed, unexpected exception has been thrown.");
return false;
}
if (exception != null && !Type.Equals(exceptionType, exception.GetType()))
{
TestHarness.TestLog("AddParticipants failed, exceptions types do not match.");
return false;
}
TestHarness.TestLog("AddParticipants succeeded");
return true;
}
示例2: DownloadTraining
/// <summary>
/// Use for downloading all images for preview
/// <param name = "_trainingIndex">current training index</param>
/// </summary>
public void DownloadTraining(int _trainingIndex)
{
Barrier barrier = new Barrier(1);
//string newDir = CreateDir(dir, _patient.AccountId.ToString());
////create treatment dir (it if needed)
//newDir = CreateDir(newDir, _patient.PatientTreatment.TreatmentNumber.ToString());
////create training dir (it if needed)
//newDir = CreateDir(newDir, _patient.PatientTreatment.TrainingList[_trainingIndex].TrainingId.ToString());
string newDir = CreateDir(dir, "trainingThumb");
foreach (int key in _patient.PatientTreatment.TrainingList[_trainingIndex].Playlist.Keys)
{
Task downloadThread = new Task(() => {
int keyThread = key;
Exercise exercise = _patient.PatientTreatment.TrainingList[_trainingIndex].Playlist[keyThread][0];
string imagePath = newDir + "\\" + exercise.ExerciseId + ".png";
string _from = exercise.ExerciseThumbs;
if (!File.Exists(imagePath))
{
barrier.AddParticipants(1);
DownloadFile(_from, imagePath);
barrier.SignalAndWait();
}
foreach (Exercise _exercise in _patient.PatientTreatment.TrainingList[_trainingIndex].Playlist[keyThread])
{
_exercise.ExerciseThumbs = imagePath;
}
});
downloadThread.Start();
}
barrier.SignalAndWait();
//barrier.Dispose();
}
示例3: BarrierSample
static void BarrierSample()
{
int count = 0;
// Create a barrier with three participants
// Provide a post-phase action that will print out certain information
// And the third time through, it will throw an exception
Barrier barrier = new Barrier(3, (b) =>
{
Console.WriteLine("Post-Phase action: count={0}, phase={1}", count, b.CurrentPhaseNumber);
Console.WriteLine(b.ParticipantCount);
//if (b.CurrentPhaseNumber == 2) throw new Exception("D'oh!");
});
// Nope -- changed my mind. Let's make it five participants.
barrier.AddParticipants(7);
// Nope -- let's settle on four participants.
//barrier.RemoveParticipant();
// This is the logic run by all participants
Action action = () =>
{
Interlocked.Increment(ref count);
barrier.SignalAndWait(); // during the post-phase action, count should be 4 and phase should be 0
/*Interlocked.Increment(ref count);
barrier.SignalAndWait(); // during the post-phase action, count should be 8 and phase should be 1
// The third time, SignalAndWait() will throw an exception and all participants will see it
Interlocked.Increment(ref count);
try
{
barrier.SignalAndWait();
}
catch (BarrierPostPhaseException bppe)
{
Console.WriteLine("Caught BarrierPostPhaseException: {0}", bppe.Message);
}
// The fourth time should be hunky-dory
Interlocked.Increment(ref count);
barrier.SignalAndWait(); // during the post-phase action, count should be 16 and phase should be 3*/
};
Action[] z=new Action[10];
for (int i=0;i<10;i++)
{
z[i] = action;
}
// Now launch 4 parallel actions to serve as 4 participants
//Parallel.Invoke(z);
Parallel.For(1,400, i=>
{
SendingRandomMessages(n);
Interlocked.Increment(ref n);
});
// This (5 participants) would cause an exception:
// Parallel.Invoke(action, action, action, action, action);
// "System.InvalidOperationException: The number of threads using the barrier
// exceeded the total number of registered participants."
// It's good form to Dispose() a barrier when you're done with it.
//barrier.Dispose();
}