我可以使用过多的后台工作线程吗? [英] Can I use too many background worker threads?
问题描述
每次我的代码需要与网络或数据库对话时,我都会使用后台工作程序,我可以使用太多吗?执行这些任务的正确方法是什么?
Each time my code needs to talk to the network or a database I use a backgroundworker, can I use too many, what is the correct way of doing these tasks?
如果我不使用后台工作程序,则在远程主机关闭等情况下,gui会锁定,因此使用后台工作程序是我知道解决此问题的唯一方法.
If I don't use a background worker the gui locks up if a remote host is down etc so using a backgroundworker is the only way I know to fix this.
我是自学成才的,所以我要不断学习,这要归功于所有人的回答.
I'm self taught so I'm learning as I go along, thanks to all who answer.
推荐答案
是的,您可以使用太多. BackgroundWorker使用线程池中的线程,因此,如果同时启动太多线程,则会耗尽线程池.
Yes, you can use too many. BackgroundWorker uses threads from the threadpool, so if you start too many (simultaneously) you will exhaust the thread pool.
Background Worker用于长时间运行的操作,而不是可能偶尔阻塞的短时间操作.改用非阻塞I/O:
Background worker is meant for long running operations, not short operations that might occasionally block. Use non-blocking I/O instead:
- BeginConnect
- BeginRead
这篇关于我可以使用过多的后台工作线程吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!