更新时间:2022-12-15 21:42:51
何时使用Task.Run,何时使用异步等待以及何时将其组合使用
When to use Task.Run, when to use async- await and when to use them in combination
我有一个关于该主题的整个博客系列,但总而言之:
I have an entire blog series on the subject, but in summary:
Task.Run
将CPU绑定(或阻止)工作移出UI线程. async
/ await
进行I/O绑定工作.Task.Run
to move CPU-bound (or blocking) work off of the UI thread for apps with a UI.async
/await
for I/O-bound work.在其他一些罕见的情况下,使用 Task.Run
或 async
很有用,但是以上两点为大多数情况提供了指导.
There are some more rare cases where Task.Run
or async
can be useful, but the two points above provide guidance for most situations.
Task.Run是否意味着后台线程将充当阻塞线程?
Does Task.Run means background thread will behave as a blocking thread?
Task.Run
实际上可以同步或异步运行,但是它确实使用线程池线程.
Task.Run
can actually run synchronously or asynchronously, but it does use thread pool threads.
我正在尝试在.net 4.5中创建桌面控制台应用程序
i am trying to create a desktop console application in .net 4.5
尝试从Internet下载多个大图像.我应该如何结合使用这些关键字,为什么?
Trying to download multiple large images from internet. How should i use these keywords in combination and why?
这是非UI应用程序中受I/O约束的操作,因此,您绝对应该首选 async
/ await
.
This is an I/O-bound operation in a non-UI app, so you should definitely prefer async
/await
.