How to get awaitable Thread.Sleep?

前端 未结 1 1417
时光取名叫无心
时光取名叫无心 2020-11-29 21:51

I\'m writing a network-bound application based on await/sleep paradigm.

Sometimes, connection errors happen, and in my experience it pays to wait for some time and

相关标签:
1条回答
  • 2020-11-29 22:25

    The other answers suggesting starting a new thread are a bad idea - there's no need to do that at all. Part of the point of async/await is to reduce the number of threads your application needs.

    You should instead use Task.Delay which doesn't require a new thread, and was designed precisely for this purpose:

    // Execution of the async method will continue one second later, but without
    // blocking.
    await Task.Delay(1000);
    
    0 讨论(0)
提交回复
热议问题