This question is a follow-up to a previous question that I had asked:
How to Perform Multiple "Pings" in Parallel using C#
I was able to get the ac
It's freezing because WaitAll
waits on all of the tasks, and you're in the UI thread, so that's blocking the UI thread. Blocking the UI thread freezes your application.
What you want to do, since you're in C# 5.0, is await Task.WhenAll(...)
instead. (You'll also need to mark that event handler as async
in it's definition.) You won't need to change any other aspects of the code. That will work just fine.
await
won't actually "wait" in the tasks. What it will do is, when it hits the await, it will wire up a continuation to the task you are await
ing on (in this case, the when all) and in that continuation it will run the remainder of the method. Then, after wiring up that continuation, it will end the method and return to the caller. This means that the UI thread isn't blocked, since this click event will end right away.
(Upon request) If you want to solve this using C# 4.0 then we'll need to start by writing WhenAll
from scratch, since it was added in 5.0. Here is what I just whipped up. It's probably not quite as efficient as the library implementation, but it should work.
public static Task WhenAll(IEnumerable<Task> tasks)
{
var tcs = new TaskCompletionSource<object>();
List<Task> taskList = tasks.ToList();
int remainingTasks = taskList.Count;
foreach (Task t in taskList)
{
t.ContinueWith(_ =>
{
if (t.IsCanceled)
{
tcs.TrySetCanceled();
}
else if (t.IsFaulted)
{
tcs.TrySetException(t.Exception);
}
else //competed successfully
{
if (Interlocked.Decrement(ref remainingTasks) == 0)
tcs.TrySetResult(null);
}
});
}
return tcs.Task;
}
Here is another option based on this suggestion in the comments by svick.
public static Task WhenAll(IEnumerable<Task> tasks)
{
return Task.Factory.ContinueWhenAll(tasks.ToArray(), _ => { });
}
Now that we have WhenAll
we just need to use that, as well as continuations, instead of await
. Instead of WaitAll
you'll use:
MyClass.WhenAll(pingTasks)
.ContinueWith(t =>
{
foreach (var pingTask in pingTasks)
{
//pingTask.Result is whatever type T was declared in PingAsync
textBox1.Text += Convert.ToString(pingTask.Result.RoundtripTime) + Environment.NewLine;
}
}, CancellationToken.None,
TaskContinuationOptions.None,
//this is so that it runs in the UI thread, which we need
TaskScheduler.FromCurrentSynchronizationContext());
Now you see why the 5.0 option is prettier, and this is a reasonably simple use case too.