加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 异步等待和线程

发布时间:2020-12-15 05:38:59 所属栏目:百科 来源:网络整理
导读:参见英文答案 async – stay on the current thread?3个 我正在使用async-await和tasks,但我无法理解一件事: 异步任务是在单独的线程中执行的吗? 正如msdn所说(Asynchronous programming): The async and await keywords don’t cause additional threads
参见英文答案 > async – stay on the current thread?3个
我正在使用async-await和tasks,但我无法理解一件事:

异步任务是在单独的线程中执行的吗?

正如msdn所说(Asynchronous programming):

The async and await keywords don’t cause additional threads to be created. Async methods don’t require multithreading because an async method doesn’t run on its own thread.

但是在ThreadPool类(ThreadPool Class)的描述中的备注:

Examples of operations that use thread pool threads include the following:

When you create a Task or Task object to perform some task asynchronously,by default the task is scheduled to run on a thread pool thread.

所以,现在我不明白异步任务是否使用单独的线程.请解释一下.谢谢.

解决方法

任务不一定代表额外的线程.

如果等待任务,则将控制流返回给方法的调用方,直到“某人”将任务设置为已完成.

如果通过Task.Run()或Task.Factory.StartNew()启动任务,则传递给这些调用的操作将在另一个线程上执行(不是新线程,而是来自ThreadPool的线程).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读