Task Parallel Library Code Freezes in a Windows Fo

2019-02-13 21:10发布

问题:

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 accepted answer (a Windows console application) to work, but when I tried to run the code in a Windows forms application, the following code will freeze on the line containing Task.WaitAll(pingTasks.ToArray()). Here is the code that I am trying to run:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.NetworkInformation;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            List<String> addresses = new List<string>();

            for (Int32 i = 0; i < 10; ++i) addresses.Add("microsoft.com");

            List<Task<PingReply>> pingTasks = new List<Task<PingReply>>();
            foreach (var address in addresses)
            {
                pingTasks.Add(PingAsync(address));
            }

            //Wait for all the tasks to complete
            Task.WaitAll(pingTasks.ToArray());

            //Now you can iterate over your list of pingTasks
            foreach (var pingTask in pingTasks)
            {
                //pingTask.Result is whatever type T was declared in PingAsync
                textBox1.Text += Convert.ToString(pingTask.Result.RoundtripTime) + Environment.NewLine;

            }

        }

        private Task<PingReply> PingAsync(string address)
        {
            var tcs = new TaskCompletionSource<PingReply>();
            Ping ping = new Ping();
            ping.PingCompleted += (obj, sender) =>
            {
                tcs.SetResult(sender.Reply);
            };
            ping.SendAsync(address, new object());
            return tcs.Task;
        }

    }

}

Does anyone have any ideas as to why it's freezing?

回答1:

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 awaiting 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.