Which task is called this example?
new Thread(() => {
Console.WriteLine("AAA");
Task.Delay(5000);
Console.WriteLine("BBB"); }).Start() ;
When we call thread's sleep, we call current thread and what's up with task.Delay if we do not create new task as my example?
In this example I call Task.Delay() without create Task.
Task.Delay
doesn't delay the "current task", it creates a new task that completes after the specified delay. It also doesn't block and will return immediately, unlike Thread.Sleep
.
Using Task.Delay
without await
, ContinueWith
or other similar methods that either wait for the completion or schedule a continuation is not useful at all.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.