[英]What does a method “do” when it awaits an empty Task?
I am outlining a class and to keep myself happy I have made a temporary return for a method 我正在概述一个课程,为了让自己高兴,我已经为一个方法做了一个临时的回报
public override Task DoPostProcessing()
{
return Task.Factory.StartNew(() => { ;} );
}
If I were to call this method and await
it, what "happens"? 如果我打电话给这个方法并
await
它,会发生什么? Is this Task
optimized away at compile time or is it ran? 此
Task
是在编译时优化还是运行?
It is run. 它运行。 It will schedule a method on a thread pool that just returns immediately.
它将在线程池上安排一个方法,该方法只是立即返回。
On a side note, do not use StartNew
; 另外, 请不要使用
StartNew
; it is dangerous . 这很危险 。 Use
Task.Run
to run code on a thread pool thread, or just use Task.FromResult
to return an already-completed task. 使用
Task.Run
在线程池线程上运行代码,或者只使用Task.FromResult
返回已完成的任务。 Task.FromResult
should be your go-to choice for noop implementations. Task.FromResult
应该是noop实现的首选。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.