我有一个带有 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、...),但它不起作用。
有谁知道如何解决这个问题?