异步/等待VS主题 [英] Async/Await vs Threads
问题描述
在净4.5微软已经添加了新的异步/待机
功能来简化异步代码。但是,我不知道
- 能
异步/待机
完全替代使用的老办法主题
? - 是
异步/待机
能够做任何一个发的
可以做 异步? - 可以
异步/待机
只能用像WebClient.DownloadStringAsync
一些方法使用或我可以将任何同步方法使之使用异步/待机
,而不是阻止主线程?
来完全取代使用线程的老路?
没有。一个线程可以做更多有益的事情。等待是专门设计来处理的的东西的拍摄时间,最典型的I / O请求。传统上做一个回调时的I / O请求完成。写作code,它依赖于这些回调是相当困难的,等待着大大简化了。
能够做什么都一个线程可以做异步的?
粗略。等待只是负责处理延迟,它不以其他方式做任何一个线程执行。该计谋的 EX pression 的,有什么在的await关键字的权利,是可以完成任务。
只能用像WebClient.DownloadStringAsync一些方法可以使用
没有。您可以使用它返回一个任务的任何方法。该XxxxAsync()方法只是$ P $,在.NET框架需要时间的常用操作pcooked的。就像从Web服务器下载数据。
In .Net 4.5 Microsoft has added the new Async/Await
feature to simplify asynchronous coding. However, I wonder
- Can
Async/Await
completely replace the old way of usingThreads
? - Is
Async/Await
capable of doing whatever aThread
can do asynchronously? - Can
Async/Await
only be used with some methods likeWebClient.DownloadStringAsync
or can I convert any synchronous method to make it useAsync/Await
and not to block the main thread?
came to completely replace the old way of using Threads ?
No. A thread can do many more useful things. Await is specifically designed to deal with something taking time, most typically an I/O request. Which traditionally was done with a callback when the I/O request was complete. Writing code that relies on these callbacks is quite difficult, await greatly simplifies it.
capable of doing what ever a Thread can do asynchronously ?
Roughly. Await just takes care of dealing with the delay, it doesn't otherwise do anything that a thread does. The await expression, what's at the right of the await keyword, is what gets the job done.
only can be used with some methods like WebClient.DownloadStringAsync
No. You can use it with any method that returns a Task. The XxxxAsync() methods are just precooked ones in the .NET framework for common operations that take time. Like downloading data from a web server.
这篇关于异步/等待VS主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!