public AddParticipants ( int participantCount ) : long | ||
participantCount | int | The number of additional participants to add to the /// barrier. |
return | long |
/// <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; }
/// <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(); }
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(); }