Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

task completion

I have a loop that creates multiple tasks as shown below. How do I update the screen (add a new line to a textbox with some data) as each task completes?

How do I detect when all tasks are complete?

C# Code

 for (int i = 0; i < int.Parse(radTextBoxFloodRequests.Text); i++)
 {
      int x = i;
      // Create a task and supply a user delegate by using a lambda expression. 
      var taskA = new Task(() => TaskRequest(int.Parse(radTextBoxFirstNumber.Text), int.Parse(radTextBoxSecondNumber.Text), int.Parse(radTextBoxFloodDelay.Text), x));
      // Start the task.
      taskA.Start();
 }


 private void TaskRequest(int number1, int number2, int delay, int count)
 {
      // Some long running method
 }
like image 791
user1438082 Avatar asked Feb 10 '13 18:02

user1438082


1 Answers

You can use ContinueWith():

"Creates a continuation that executes asynchronously when the target Task completes." - MSDN

Task t = new Task(() => Console.WriteLine("")).ContinueWith(task => Console.Writeline("Continue With"), 
                                                            TaskScheduler.FromCurrentSynchronizationContext());
like image 133
User 12345678 Avatar answered Oct 20 '22 15:10

User 12345678