如何在不阻止UI线程的情况下等待所有任务完成?

Pab*_*blo 3 c# task winforms task-parallel-library

在下面的代码中,我在处理任务之前禁用按钮,并希望在所有任务完成后启用它.

List<Task> tasks = new List<Task>();
buttonUpdateImage.Enabled = false; // disable button
foreach (OLVListItem item in cellsListView.CheckedItems)
{
    Cell c = (Cell)(item.RowObject);

    var task = Task.Factory.StartNew(() =>
    {
        Process p = new Process();
        ...
        p.Start();
        p.WaitForExit();
    });
    task.ContinueWith(t => c.Status = 0);
    tasks.Add(task);
}

Task.WaitAll(tasks.ToArray());
// enable button here
Run Code Online (Sandbox Code Playgroud)

WaitAll阻止UI线程.我怎么能等到所有任务完成然后启用按钮?

Yuv*_*kov 5

首先,我安装了Microsoft.Bcl.Async哪些可以async-await在.NET 4.0中使用.

现在,使用这个问题的答案,您可以异步注册进程退出,而无需使用Task.Factory.StartNew:

public static class ProcessExtensions
{
    public static Task RunProcessAsync(this Process process, string fileName)
    {
        if (process == null)
            throw new ArgumentNullException(nameof(process));

        var tcs = new TaskCompletionSource<bool>();
        process.StartInfo = new ProcessStartInfo
        {
            FileName = fileName 
        };

        process.EnableRaisingEvents = true
        process.Exited += (sender, args) =>
        {
            tcs.SetResult(true);
            process.Dispose();
        };

        process.Start();
        return tcs.Task;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,你可以这样做:

buttonUpdateImage.Enabled = false; // disable button

var tasks = cellsListView.CheckedItems.Cast<OLVListItem>()
                                      .Select(async item => 
{
    Cell cell = (Cell)item.RowObject;

    var process = new Process();
    await process.RunProcessAsync("path");

    cell.Status = 0;
});

await Task.WhenAll(tasks);
buttonUpdateImage.Enabled = true;
Run Code Online (Sandbox Code Playgroud)