C# 异步/等待 vs 线程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15148852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:15:54  来源:igfitidea点击:

Async/Await vs Threads

c#.net

提问by Roman Ratskey

In .Net 4.5 Microsoft has added the new Async/Awaitfeature to simplify asynchronous coding. However, I wonder

在 .Net 4.5 中,Microsoft 添加了新Async/Await功能以简化异步编码。然而,我想知道

  1. Can Async/Awaitcompletely replace the old way of using Threads?
  2. Is Async/Awaitcapable of doing whatever a Threadcan do asynchronously?
  3. Can Async/Awaitonly be used with some methods like WebClient.DownloadStringAsyncor can I convert any synchronous method to make it use Async/Awaitand not to block the main thread?
  1. Async/Await完全替代旧的使用方式 Threads吗?
  2. Async/Await能够做任何一个的Thread可以异步呢?
  3. 可以Async/Await只与像一些方法来使用WebClient.DownloadStringAsync或者我可以将任何同步方法,使其使用Async/Await和不阻塞主线程?

采纳答案by Hans Passant

can it completely replace the old way of using Threads ?

它可以完全取代使用 Threads 的旧方式吗?

No. A thread can do many more useful things. Await is specifically designed to deal with somethingtaking 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.

不。一个线程可以做更多有用的事情。Await 专门设计用于处理一些耗时的事情,最典型的是 I/O 请求。传统上,当 I/O 请求完成时,这是通过回调完成的。编写依赖这些回调的代码非常困难,await 大大简化了它。

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. Ideally it doesn't use a thread at all, it posts a driver request and once the driver completes the data transfer it generates a completion notification callback. Networking is by far the most common usage, latencies of hundreds of milliseconds are common and an inevitable side-effect of services moving from the desktop or a LAN into "the cloud". Using such services synchronously would make a UI quite unresponsive.

大致。Await 只负责处理延迟,否则它不会执行线程所做的任何事情。await 关键字右侧的 await表达式用于完成工作。理想情况下,它根本不使用线程,它会发布一个驱动程序请求,一旦驱动程序完成数据传输,它就会生成一个完成通知回调。网络是迄今为止最常见的用途,数百毫秒的延迟很常见,并且是服务从桌面或 LAN 转移到“云”的不可避免的副作用。同步使用此类服务​​会使 UI 完全没有响应。

only can be used with some methods like WebClient.DownloadStringAsync

只能与某些方法一起使用,例如 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.

不可以。您可以将它与任何返回任务的方法一起使用。XxxxAsync() 方法只是 .NET 框架中针对需要时间的常见操作的预制方法。就像从 Web 服务器下载数据一样。

回答by wRAR

The official statement on this.Though you should understand the differences between threads and asynchronous programming before blindly replacing one things with others.

官方就此发表声明。尽管在盲目地将一件事替换为另一件事之前,您应该了解线程和异步编程之间的区别。

回答by xflowXen

I think about it this way (and I think Microsoft does too if you look at https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2012/hh191443(v=vs.110)#threads)

我是这样考虑的(如果您查看https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2012/hh191443(v=vs.110) )#线程)

Async/await is a quick way to run some code on the main application thread with the advantage that the code can suspend itself when it has no work to do and return focus to the main thread, "wake up" on the main thread when there is a result to be obtained and then pass processing back to - you guessed it - the main thread. Think of it like an event based GOTO statement in Basic that can pass control back and forth to a specific line of execution.

Async/await 是一种在主应用程序线程上运行一些代码的快速方法,其优点是代码可以在没有工作时挂起自己并将焦点返回到主线程,当有工作时在主线程上“唤醒”是要获得的结果,然后将处理传递回 - 你猜对了 - 主线程。可以把它想象成 Basic 中基于事件的 GOTO 语句,可以将控制权来回传递给特定的执行行。

In contrast a thread is a separate stream of execution that can run with its own variables etc. where - given sufficient hardware - execution occurs in parallel to the main thread.

相比之下,线程是一个单独的执行流,可以使用它自己的变量等运行。其中 - 给定足够的硬件 - 执行与主线程并行发生。

If you have a GUI application that is going to download a single file and then do something with that file when its downloaded - I'd implement that using an async/await method.

如果您有一个 GUI 应用程序要下载单个文件,然后在下载该文件时对该文件执行某些操作 - 我将使用 async/await 方法实现它。

However if your GUI needs to download 5000 files - I'd create a file download thread to handle that since the main GUI thread may freeze while execution is transferred to handle downloading the files.

但是,如果您的 GUI 需要下载 5000 个文件 - 我会创建一个文件下载线程来处理该问题,因为主 GUI 线程可能会在执行转移以处理下载文件时冻结。