/// <summary> /// Download all the images for the current session /// </summary> public void DownloadAllTreatmentImages() { Barrier barrier = new Barrier(_patient.PatientTreatment.TrainingList.Count + 2); Task treatmentThread = new Task(() => { //Downloading all thumbs in treatment DownloadTreatment(); barrier.SignalAndWait(); }); treatmentThread.Start(); foreach(Training t in _patient.PatientTreatment.TrainingList) { Task tt = new Task(() => { DownloadTraining(_patient.PatientTreatment.TrainingList.IndexOf(t)); barrier.SignalAndWait(); }); tt.Start(); } barrier.SignalAndWait(); barrier.Dispose(); }
public async Task ExecuteTasksSequentiallyAndInOrder() { using (var testSubject = new MissionControl()) { var fool = testSubject.SpawnFool(); var sentMessage = new SimpleTestMessage(); SimpleTestMessage receivedMessage = null; var barrier = new Barrier(2); var first = fool.DoWork( sentMessage, msg => { barrier.SignalAndWait(); receivedMessage = msg; }); var second = fool.DoWork(sentMessage, msg => { barrier.SignalAndWait(); }); var done = Task.WhenAll(first, second) .ContinueWith( t => { barrier.Dispose(); return Work.Completed; }); var timeout = Task.Delay(100.Milliseconds()) .ContinueWith(t => Work.TimedOut); var result = await await Task.WhenAny(done, timeout); result.Should() .Be(Work.TimedOut); } }
public void Start() { _barrier = new Barrier(2); _worker = new Thread(Worker); _worker.Start(); _barrier.SignalAndWait(); _barrier.Dispose(); _barrier = null; }
public static bool CancelBeforeWait() { TestHarness.TestLog("* BarrierCancellationTests.CancelBeforeWait()"); bool passed = true; Barrier barrier = new Barrier(3); CancellationTokenSource cs = new CancellationTokenSource(); cs.Cancel(); CancellationToken ct = cs.Token; const int millisec = 100; TimeSpan timeSpan = new TimeSpan(100); passed &= TestHarnessAssert.EnsureOperationCanceledExceptionThrown(() => barrier.SignalAndWait(ct), ct, "An OCE should have been thrown."); passed &= TestHarnessAssert.EnsureOperationCanceledExceptionThrown(() => barrier.SignalAndWait(millisec, ct), ct, "An OCE should have been thrown."); passed &= TestHarnessAssert.EnsureOperationCanceledExceptionThrown(() => barrier.SignalAndWait(timeSpan, ct), ct, "An OCE should have been thrown."); barrier.Dispose(); return passed; }
public static void BarrierCancellationTestsCancelBeforeWait() { Barrier barrier = new Barrier(3); CancellationTokenSource cs = new CancellationTokenSource(); cs.Cancel(); CancellationToken ct = cs.Token; const int millisec = 100; TimeSpan timeSpan = new TimeSpan(100); EnsureOperationCanceledExceptionThrown( () => barrier.SignalAndWait(ct), ct, "CancelBeforeWait: An OCE should have been thrown."); EnsureOperationCanceledExceptionThrown( () => barrier.SignalAndWait(millisec, ct), ct, "CancelBeforeWait: An OCE should have been thrown."); EnsureOperationCanceledExceptionThrown( () => barrier.SignalAndWait(timeSpan, ct), ct, "CancelBeforeWait: An OCE should have been thrown."); barrier.Dispose(); }
private void BtnAll_Click(object sender, EventArgs e) { EnableUI(false); _sorterRunning = int.MaxValue; bool fast = ModifierKeys == Keys.Shift; _cts = new CancellationTokenSource(); var barrier = new Barrier(0, DrawArrays); var tasks = new List<Task>(_sorters.Count) { // Dummy task, so that the continuation below is run for sure. Task.Run(() => { return; }), }; var ui = SynchronizationContext.Current; for (int i = 0; i < _sorters.Count; ++i) { var sorter = _sorters[i]; if (sorter.Run) { sorter.Abort = _cts.Token; if (!fast) { sorter.SteppedExecution = barrier; sorter.SteppedExecution.AddParticipant(); } sorter.Numbers = _numbers; tasks.Add(Task.Factory.StartNew(() => { sorter.Sort(); }, _cts.Token, TaskCreationOptions.LongRunning, TaskScheduler.Current)); } } Task.WhenAll(tasks).ContinueWith(_ => { ui.Post(delegate { EnableUI(true); DrawArrays(null); DgvSorters.Refresh(); }, null); barrier.Dispose(); _cts.Dispose(); _cts = null; }); }
/// <summary> /// Download training preview images for the treatment screen /// </summary> public void DownloadTreatment() { //crearte dir for user (if not already created) //string newDir = CreateDir(dir, _patient.AccountId.ToString()); //newDir = CreateDir(newDir, _patient.PatientTreatment.TreatmentNumber.ToString()); string newDir = CreateDir(dir, "treatmentThumb"); //initilaize the barrier int size = _patient.PatientTreatment.TrainingList.Count; //Barrier _barrier = new Barrier(size + 1); Barrier _barrier = new Barrier(1); for (int i = 0; i < size; i++) { int temp = i; string _from = _patient.PatientTreatment.TrainingList[temp].TrainingThumbs; string _to = newDir + "\\" + _patient.PatientTreatment.TrainingList[temp].TrainingId + ".png"; _patient.PatientTreatment.TrainingList[temp].TrainingThumbs = _to; if (!File.Exists(_to)) { _barrier.AddParticipant(); Task tempThread = new Task(() => { DownloadFile(_from, _to); _barrier.SignalAndWait(); }); tempThread.Start(); } } _barrier.SignalAndWait(); _barrier.Dispose(); }
private void TerminateWorker() { _barrier = new Barrier(2); _terminate = true; _barrier.SignalAndWait(); _barrier.Dispose(); _barrier = null; }
/// <summary> /// Test Dispose /// </summary> /// <returns>Tru if the test succeeded, false otherwise</returns> private static bool RunBarrierTest6_Dispose() { TestHarness.TestLog("*RunBarrierTest6_Dispose"); Barrier b = new Barrier(1); b.Dispose(); try { b.SignalAndWait(); } catch (ObjectDisposedException) { TestHarness.TestLog("Dispose succeeded"); return true; } TestHarness.TestLog("Cancel failed, SignalAndWait didn't throw exceptions after Dispose."); return false; }
/// <summary> /// Parallel Dijkstra algorithm /// </summary> /// /// <param name="graph"> /// <see cref="Graph"/> object to check the path for /// </param> /// /// <param name="start"> /// Start /// </param> /// /// <returns> /// List of <see cref="Link"/> objects /// </returns> private static List<Link> Dijkstra_p(Graph graph, int start) { List<Vertex> P = (from v in graph.Vertices select new Vertex(v, graph.Links)).ToList(); var qT = from vertex in P where vertex.ID != start orderby vertex.ID ascending select vertex; Queue<Vertex> Q = new Queue<Vertex>(qT); Vertex curr = P[start]; curr.closest_distance = 0.0f; while (Q.Count > 0) { Barrier barrier = new Barrier(curr.neighbors.Count + 1); foreach (Tuple<int, double> n in curr.neighbors) { var tuple = n; ThreadPool.QueueUserWorkItem( obj => { Vertex neighbor = P[tuple.Item1]; double dist = tuple.Item2 + curr.closest_distance; bool contains = (from v in Q where v.ID == tuple.Item1 select v).Count() > 0; if (contains && (dist < neighbor.closest_distance)) { neighbor.closest = curr; neighbor.closest_distance = dist; } barrier.SignalAndWait(); }, null); } barrier.SignalAndWait(); barrier.Dispose(); curr = Q.Dequeue(); } return (from v in P where v.ID != start select new Link(v.ID, v.closest.ID, v.closest_distance)).ToList(); }
public void NotifyCallQueueOnEveryUserButPostingUser() { var service = GetService(); var topic = new Topic(123); var user = new User(768, DateTime.MinValue); var list = new List<User> { user, new User(2, DateTime.MinValue) }; _mockSubRepo.Setup(s => s.GetSubscribedUsersThatHaveViewed(topic.TopicID)).Returns(list); var topicLink = "foo"; Func<User, string> gen = u => "x" + u.UserID; var barrier = new Barrier(1); Action action = () => { service.NotifySubscribers(topic, user, topicLink, gen); barrier.SignalAndWait(); }; action.Invoke(); barrier.Dispose(); _mockSubTopicEmail.Verify(s => s.ComposeAndQueue(topic, It.IsAny<User>(), topicLink, It.IsAny<string>()), Times.Exactly(1)); _mockSubTopicEmail.Verify(s => s.ComposeAndQueue(topic, list[0], topicLink, "x" + list[0].UserID), Times.Exactly(0)); _mockSubTopicEmail.Verify(s => s.ComposeAndQueue(topic, list[1], topicLink, "x" + list[1].UserID), Times.Exactly(1)); }