异步/等待VS主题主题、VS

2023-09-02 11:53:45 作者:没有现实怎么有狗

在净4.5微软已经添加了新的异步/待机功能来简化异步代码。但是,我不知道

异步/待机完全替代使用的老办法 主题? 是异步/待机能够做任何一个发的可以做 异步? 可以异步/待机只能用像 WebClient.DownloadStringAsync 一些方法使用或我可以将任何同步方法使之使用异步/待机,而不是阻止主线程? 解决方案   

来完全取代使用线程的老路?

没有。一个线程可以做更多有益的事情。等待是专门设计来处理的的东西的拍摄时间,最典型的I / O请求。传统上做一个回调时的I / O请求完成。写作code,它依赖于这些回调是相当困难的,等待着大大简化了。

  

能够做什么都一个线程可以做异步的?

粗略。等待只是负责处理延迟,它不以其他方式做任何一个线程执行。该计谋的 EX pression 的,有什么在的await关键字的权利,是可以完成任务。

  

只能用像WebClient.DownloadStringAsync一些方法可以使用​​

XP使用的win7Aero主题

没有。您可以使用它返回一个任务的任何方法。该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 using Threads? Is Async/Await capable of doing whatever a Thread can do asynchronously? Can Async/Await only be used with some methods like WebClient.DownloadStringAsync or can I convert any synchronous method to make it use Async/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.