英文:
Use async delegate inside synchronous method
问题
我对这种情况下的基本行为不太确定:
public void Test()
{
Method(async () => await Task.Delay(10000));
}
public void Method(Action action)
{
action();
}
创建的委托使用了 async/await 机制,但在 Method
内部,action
同步调用,甚至没有办法以异步方式调用它,因为该委托不返回 Task
。那么为什么能够为这个委托生成异步定义呢?乍一看,似乎只有像这样的代码才应该被允许:
Method(() => Thread.Sleep(10000));
我错过了什么吗?
英文:
I'm quite not sure about underlying behavior in this case:
public void Test()
{
Method(async () => await Task.Delay(10000));
}
public void Method(Action action)
{
action();
}
A created delegate uses async/await machinery, but inside Method
, action
is called synchronisly and even there is no way to call it in async manner since this delegate doesn't return Task
. So why it's even possible to generate async definition for this delegate? At first glance, only code like:
Method(() => Thread.Sleep(10000));
should be allowed. What did I miss?
答案1
得分: 2
您的lambda表达式正在转换为一个没有返回值的async void方法。您可以通过编写以下方式来证明这一点:
static async void Foo()
{
}
static async Task Bar()
{
}
public void Test()
{
Method(Foo); //有效
Method(Bar); //编译错误
}
这意味着它将作为“fire and forget”调用运行。
值得注意的是,除非特定情况,最好避免使用async void
。请参阅这个答案。
英文:
Your lambda expression is being converted into an async void method which has no return value. You can prove this by writing it out:
static async void Foo()
{
}
static async Task Bar()
{
}
public void Test()
{
Method(Foo); //Works
Method(Bar); //Compilation error
}
This means it will run as a "fire and forget" call.
It's worth noting that async void
is best avoided except for specific cases. See this answer.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论