I just started to touch the thread task of C#. I hope that whenever I click the button, I add a task, and then wait for all tasks to be completed before proceeding to the next step.
My code is as follows:
List<Task> SingleTaskList = new List<Task>();
private void Add_Click(object sender, RoutedEventArgs e)
{
Task t = Task.Factory.StartNew(() => { Dosomething();});
SingleTaskList.Add(Task.Factory.StartNew(() => { Dosomething(); }));
Task.Factory.ContinueWhenAll(SingleTaskList.ToArray(), tArray =>
{
Dispatcher.Invoke(() =>
{
//Update the UI
});
});
}
But I found that when any one of the tasks is completed, the next step will be taken.
How should I modify this code?