c# - 查询任务的 Result 属性导致线程池创建一个新线程

标签 c# .net multithreading

这里引用了 Jeffrey Richter 的“CLR via C#”一书:

** Starting a New Task Automatically When Another Task Completes**

...calling Wait or querying a task’s Result property when the task has not yet finished running will most likely cause the thread pool to create a new thread, which increases resource usage and hurts scalability.

线程池为什么要新建线程?

最佳答案

在 native 代码中,您首先会使用 CreateThreadpoolWork 创建一个工作项然后使用 WaitForThreadpoolWorkCallbacks 等待它功能。

在 .NET 世界中,我会使用 TPL,创建一个任务,然后是一个 continuation task .将其与 TPL 相结合 TaskScheduler应该会给你想要的效果。

关于c# - 查询任务的 Result 属性导致线程池创建一个新线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5566017/

相关文章:

c# - 到 "broadcast"到 TCP 客户端列表的最快方法

c# - 大型接口(interface)的统一错误处理

c# - 什么是.Net中的序列化

java - LibGDX:在另一个线程中创建新的 scene2d 对象?

c# - 如何将字典拆分为 "n"部分

c# - 没有 INotifyPropertyChanged 的​​更改通知? (摘自 Pro WPF in C# 2010)

c# - 文件助手源代码

python - 使用 TCP 客户端在 Python 中发送多条消息

java - 在java中登录多线程应用程序

c# - SOA、TDD、DI 和 DDD - 一个 shell 游戏?