In order for something to be awaited, it has to be awaitable. As void
is not so, you cannot await on any Action
delegate.
An awaitable is any type that implements a GetAwaiter
method, which returns a type that implements either INotifyCompletion
or ICriticalNotifyCompletion
, like Task
and Task<T>
, for example.
If you want to wait on a delegate, use Func<Task>
, which is an equivalent to a named method with the following signature:
public Task Func()
So, in order to await, change your method to:
static void Main(string[] args)
{
Func<Task> testFunc = async () =>
{
Console.WriteLine("In");
await Task.Delay(100);
Console.WriteLine("First delay");
await Task.Delay(100);
Console.WriteLine("Second delay");
};
}
And now you can await it:
await testFunc();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…