async await WhenAll not waiting

时间:2015-12-03 14:39:30

标签: c# .net winforms async-await

我在VS2013,.NET FW 4.5.1中开发WinForms应用程序。这是我的简化代码,其中包含有关结构的内联注释:

// Progress object implementing IProgress<MyProgressData>
var progressCallback = new Progress<MyProgressData>();

// listOfMyList is actually List<List<MyObject>>, which contains list of
// list of MyObject's which will be executed as tasks at once.
// For example, this would be sample structure for list of lists:
// List1
//   MyObject1
//   MyObject2
//   MyObject3
// List2
//   MyObject4
//   MyObject5
//   MyObject6
// List1's and List2's objects would be executed as all tasks at once, but List1 and List2 respectively
// would be executed one after another (because of resources usage inside TASK CODE)
foreach (var myItem in listOfMyList)
{
  var myList = myItem.ToList();

  // Create a list of tasks to be executed (20 by default; each taking from 30-60 seconds)
  // Here cs is actually MyObject
  var myTasks = myList.Select(cs => Task.Run(async () =>
  {
    // TASK CODE (using cs as an input object and using "cancellationToken.ThrowIfCancellationRequested();" inside execution to cancel executing if requested)
  }, cancellationToken));

  await Task.WhenAll(myTasks); // Wait for all tasks to finish

  // Report progress to main form (this actually calls an event on my form)
  await Task.Run(() => progressCallback.Report(new MyProgressData() { props }), CancellationToken.None);
}

如您所见,我构建进度对象,然后我有列表列表。顶级列表中的每个项目应以序列化方式(一个接一个)执行。每个项目的列表元素应该以任务的形式一次执行。 到目前为止一切顺利,所有任务都开始了,甚至在等待它们的时候。或者至少我是这么认为的。我已将日志记录放入相关方法中,以显示代码执行情况。事实证明,当执行逻辑(在底部)正在执行时,foreach循环开始执行另一批任务,它不应该。 我在这里错过了什么吗?进度代码是否阻止或等待Report方法完成执行。也许我错过了关于异步/等待的事情。等待我们确保代码不会继续,直到方法完成后?它不会阻止当前线程,但它也不会继续执行? 甚至可能(因为它发生了,它可能是),因为我的foreach循环继续执行而进度报告仍然在进行中?

此代码驻留在异步方法中。它实际上是这样调用的(假设这个方法是async MyProblematicMethod()):

while (true)
{
    var result = await MyProblematicMethod();
    if (result.HasToExitWhile)
        break;
}

MyProblematicMethod中的每个方法都使用await来等待异步方法,并且不会多次调用。

1 个答案:

答案 0 :(得分:0)

根据Glorin的建议,IProgress.Report在触发事件处理程序后立即返回,我已经创建了Progress类的精确副本,它使用了SyncContext.Send而不是Post:

public sealed class ProgressEx<T> : IProgress<T>
{
    private readonly SynchronizationContext _synchronizationContext;
    private readonly Action<T> _handler;
    private readonly SendOrPostCallback _invokeHandlers;

    public event EventHandler<T> ProgressChanged;

    public ProgressEx(SynchronizationContext syncContext)
    {
        // From Progress.cs
        //_synchronizationContext = SynchronizationContext.CurrentNoFlow ?? ProgressStatics.DefaultContext;
        _synchronizationContext = syncContext;
        _invokeHandlers = new SendOrPostCallback(InvokeHandlers);
    }

    public ProgressEx(SynchronizationContext syncContext, Action<T> handler)
        : this(syncContext)
    {
        if (handler == null)
            throw new ArgumentNullException("handler");
        _handler = handler;
    }

    private void OnReport(T value)
    {
        // ISSUE: reference to a compiler-generated field
        if (_handler == null && ProgressChanged == null)
            return;
        _synchronizationContext.Send(_invokeHandlers, (object)value);
    }

    void IProgress<T>.Report(T value)
    {
        OnReport(value);
    }

    private void InvokeHandlers(object state)
    {
        T e = (T)state;
        Action<T> action = _handler;
        // ISSUE: reference to a compiler-generated field
        EventHandler<T> eventHandler = ProgressChanged;
        if (action != null)
            action(e);
        if (eventHandler == null)
            return;
        eventHandler((object)this, e);
    }
}

这意味着ProgressEx.Report将在返回之前等待方法完成。也许不是所有情况下的最佳解决方案,但在这种情况下它对我有用。

要调用它,只需使用SynchronizationContext.Current创建ProgressEx作为构造函数的参数。但是,它必须在UI线程中创建,因此传入正确的SynchronizationContext。例如。 new ProgressEx<MyDataObject>(SynchronizationContext.Current)