如何创建后台工作者池? [英] How Do I Create a Pool of Background Workers?

查看:76
本文介绍了如何创建后台工作者池?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现我喜欢通过BackgroundWorkers异步运行代码的简单性.我通过示例或使用错误,陷阱,安全线程等的反复试验教给我自己,而不是理论,也许是我的致命弱点.

I find that I am enjoying the simplicity of running code asynchronously through BackgroundWorkers. I have taught myself through example or trial and error on its uses, pitfalls, safe-threading etc., not so much the theory and perhaps my Achilles heel.

我已经做了很多工作,但是我没有太多谈论的一件事,如何有效地创建和使用BackgroundWorkers池?还是有什么比使用BW更简单的更好的选择?

I have done my reserach but one thing I don't find very much talk of, how can I effectively create and use a pool of BackgroundWorkers? Or what is a better alternative that is just as simple as using BWs?

我将说明我遇到的问题: 我有一个任务X,Y和Z,每个任务都很密集,因此它将挂断UI线程.我确定,即使此代码仅执行一次,BW还是答案.我学会了如何使用它们,一些安全的线程实践,然后继续前进.不久我的代码看起来像:BW1,BW2,BW3等.

I will illustrate the problem I am running into: I had a task X, Y, and Z, each one intensive, thus it will hang up the UI thread. I decided BWs are the answer, EVEN, if this code was executed only once. I learned how to use them, some safe threading practices, and went on my way. Pretty soon my code looked like: BW1, BW2, BW3, and so forth.

然后,我开始更加熟悉它们,并真正将代码放入BW中,这些代码可用于硬件监视或其他无限循环/始终运行的项目.我仍然遇到了BW5,BW6 ....问题.

Then I began to get more familiar with them and really put in code into BWs that can be used for Hardware Monitoring or other infinite looped / always running items. I still ran into a BW5, BW6.... issue.

我发现了一种新颖的方法,可以通过在调用RunWorkerAsynchronous()之前设置一个全局字符串变量来设置具有多用途BackgroundWorkers的变量,然后我可以在BackgroundWorker中嵌套If/Elses或Switchs来执行基于代码的所需代码我将字符串变量设置为什么.但是,我觉得我只是在自我学习的极限范围内跳舞.

I found creative ways to have multipurpose BackgroundWorkers by having a Global String Variable which was set before RunWorkerAsynchronous() was called and then I could either have Nested If/Elses or Switches inside the BackgroundWorker to execute the code that was needed based off of what I set the String Variable to. However, I feel I am just dancing around a limit of my self-learning.

所以任何人都可以引导我完成创建线程池/BW的概念,并在我的程序中根据需要使用它们,回收线程而不是显式地创建每个线程,然后为它配备数十名后台工作人员.专门进行强化训练吗?

So can anyone guide me through the concept of creating a Pool of Threads/BWs and use them as needed throughout my program, recycling the threads instead of explicitly creating each one, then having dozens of backgroundworkers for specific intensive purposes to juggle?

注意:我不是一个很好的程序员,所以我正在寻找简单的功能示例或解释.竭诚欢迎任何帮助!

Note: I am not a very good programmer so I am looking for simple functional examples or explanations. Any and all help though is eagerly welcome!

推荐答案

您不需要将它们合并.他们在内部使用ThreadPool,该线程池已经是线程池.

You don't need to pool them. They use internally the ThreadPool, that is already a pool of threads.

线程/线程池或backgroundworker

这篇关于如何创建后台工作者池?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆