Ars*_*ray 120 .net c# multithreading async-await
我正在编写一个基于await/sleep范例的网络绑定应用程序.
有时会发生连接错误,根据我的经验,等待一段时间再重试操作是值得的.
问题是,如果我在await/async中使用Thread.Sleep或类似的阻塞操作,它会阻止调用程序线程中的所有活动.
我应该用什么来替换Thread.Sleep(10000)以获得相同的效果
await Thread.SleepAsync(10000)
Run Code Online (Sandbox Code Playgroud)
?
UPDATE
我更喜欢一个没有创建任何额外线程的答案
Jon*_*eet 281
其他答案表明开始一个新线程是一个坏主意 - 根本没有必要这样做.点的一部分async/ await是减少线程应用程序需要的数量.
你应该使用Task.Delay哪并不需要一个新的线程,并且正是为此而设计的:
// Execution of the async method will continue one second later, but without
// blocking.
await Task.Delay(1000);
Run Code Online (Sandbox Code Playgroud)