ThreadPool何时分配一个线程给Backgroundworker实例?
如果我没记错的话,.NET中的Backgroundworker将从CLR ThreadPool中获取一个线程,以执行后台任务。 我想实例化后台工作者对象的集合或列表。 然而,并不是集合中的所有对象都会同时被调用。 所以让我担心的是,如果我有一个包含 10 个后台工作对象的集合,CLR ThreadPool 将为我分配 10 个线程,并且我认为如果我的集合中有太多对象,它会耗尽 ThreadPool 中的线程。
所以我的问题是,ThreadPool什么时候会为BackgroundWorker的实例分配一个线程? 是在实例化对象时还是在调用方法 RunWorkerAsync() 时?
预先感谢您的所有帮助
If i am not wrong, Backgroundworker in .NET will get a thread from CLR ThreadPool, in order to perform background task. I would like to instantiate a collection or list of backgroundworker objects. However, not all the objects in the collection will be invoked in the sametime. So what worry me is that if i have a collection of 10 backgroundworkers objects, CLR ThreadPool will allocate 10 threads for me, and i think it will use up the threads in the ThreadPool if i have too many objects in my collection.
So here my question, when will ThreadPool allocates a thread to an instance of BackgroundWorker? Is it when the object is instantiated or when the method RunWorkerAsync() is called?
Thanks in advance for all of your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是您不应该真正关心的实现细节。 不要过早地优化和放弃。 也就是说,不要创建不必要的 BackgroundWorker 实例,也不要发明复杂的优化逻辑,以便您能够重用它们。 只需在需要时实例化它们即可。
This is an implementation detail you should not really be concerned about. Do not optimize nether perssimize prematurely. That is, do not create unnecessary instances of
BackgroundWorker
s and do not invent complex optimization logic whereby you'll be able to reuse those. Just instantiate them as need arises.作为您问题的答案,BackgroundWorker 线程将由 RunWorkerAsync 中的 ThreadPool 分配。
您可以研究下一个BackgroundWorker实现以了解更多其机制。
BackgroundWorker 实现 - 在 C# 中
As an answer to your question, the BackgroundWorker thread will be allocated by the ThreadPool in the RunWorkerAsync.
You can study the next BackgroundWorker implementation to understand more of its mechanism.
BackgroundWorker implementation -in C#