6

我有一个带有 ActionBlock 的类 Receiver:

public class Receiver<T> : IReceiver<T>
{

  private ActionBlock<T> _receiver;

  public Task<bool> Send(T item) 
  {
     if(_receiver!=null)
        return _receiver.SendAsync(item);

     //Do some other stuff her
  }

  public void Register (Func<T, Task> receiver)
  {
    _receiver = new ActionBlock<T> (receiver);
  }

  //...
}

ActionBlock 的 Register-Action 是带有 await-Statement 的异步方法:

private static async Task Writer(int num)
{
   Console.WriteLine("start " + num);
   await Task.Delay(500);
   Console.WriteLine("end " + num);
}

现在我想做的是同步等待(如果设置了条件),直到操作方法完成以获得独占行为:

var receiver = new Receiver<int>();
receiver.Register((Func<int, Task) Writer);
receiver.Send(5).Wait(); //does not wait the action-await here!

问题是当“等待 Task.Delay(500);” 语句被执行,“receiver.Post(5).Wait();” 不再等待。

我尝试了几种变体(TaskCompletionSource、ContinueWith、...),但它不起作用。

有谁知道如何解决这个问题?

4

1 回答 1

4

ActionBlock默认情况下将强制执行排他行为(一次只处理一项)。如果您通过“排他行为”表示其他意思,您可以TaskCompletionSource在操作完成时通知您的发件人:

... use ActionBlock<Tuple<int, TaskCompletionSource<object>>> and Receiver<Tuple<int, TaskCompletionSource<object>>>
var receiver = new Receiver<Tuple<int, TaskCompletionSource<object>>>();
receiver.Register((Func<Tuple<int, TaskCompletionSource<object>>, Task) Writer);
var tcs = new TaskCompletionSource<object>();
receiver.Send(Tuple.Create(5, tcs));
tcs.Task.Wait(); // if you must

private static async Task Writer(int num, TaskCompletionSource<object> tcs)
{
  Console.WriteLine("start " + num);
  await Task.Delay(500);
  Console.WriteLine("end " + num);
  tcs.SetResult(null);
}

或者,您可以使用AsyncLock包含在我的 AsyncEx 库中):

private static AsyncLock mutex = new AsyncLock();

private static async Task Writer(int num)
{
  using (await mutex.LockAsync())
  {
    Console.WriteLine("start " + num);
    await Task.Delay(500);
    Console.WriteLine("end " + num);
  }
}
于 2012-12-04T13:50:53.903 回答