How and when to use ‘async’ and ‘await’

前端 未结 21 1787
你的背包
你的背包 2020-11-21 05:07

From my understanding one of the main things that async and await do is to make code easy to write and read - but is using them equal to spawning background threads to perfo

21条回答
  •  逝去的感伤
    2020-11-21 05:34

    I think you've picked a bad example with System.Threading.Thread.Sleep

    Point of an async Task is to let it execute in background without locking the main thread, such as doing a DownloadFileAsync

    System.Threading.Thread.Sleep isn't something that is "being done", it just sleeps, and therefore your next line is reached after 5 seconds ...

    Read this article, I think it is a great explanation of async and await concept: http://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx

提交回复
热议问题