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
}
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With