如果它不返回任何东西,是否需要等待异步任务函数?这会导致下面的代码包装在委托中,并在异步任务函数返回后执行?如果我不在意它的返回值,我应该等待“异步任务”函数吗?
Task DoSomethingAsync()
{
return Task.Run(() =>
{
// Do something, but doesn't return values.
});
}
void Test()
{
DoSomethingAsync(); // should I await?
// Do other things, totally not related to the async function
Console.WriteLine("aaa");
}
在上面的例子中,如果我在等待测试(DoSomethingAsync()),将导致下面的代码Console.WriteLine被包裹在委托和仅推迟执行时异步任务完成?
我看不出理由。 – Trey
如果你这样调用它,它将同步运行。 – dcg
@dcg不,它不会。它会在它完成之前继续。 – Servy