如何在VB.net中动态创建后台工作者 [英] How to dynamically create a background worker in VB.net

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

问题描述

我有一个VB.net项目,它使用后台工作者来做一些事情.

I have a VB.net project which uses a background worker to do some stuff.

现在,我想扩展项目以能够做很多事情:)

Now I want to expand the project to be able to do multiple stuff :)

用户可以在文本框中输入URL,当用户单击解析"按钮时,程序将创建一个新的tabcontrol,并输出一些数据.

A user can enter an URL in a textbox and when the user click on the parse button the program creates a new tabcontrol a outputs some data.

我为此使用了硬编码的后台工作者.

I use a hardcoded background worker for this.

但是现在我想运行多个后台工作人员来完成这些工作,所以我不必依靠对后台工作人员进行硬编码.

But now I want to run multiple background workers to do this stuff so I can't rely on hard coding the background worker(s).

是否可以动态创建后台工作器.

Is it possible to create background workers dynamically.

我只是不知道如何设置它,因为我认为我需要设置不同的方法和变量,例如:

I just don't have any idea how to set this up since I think I need to set up the different methods and variables like:

Private bw As BackgroundWorker = New BackgroundWorker
bw.WorkerReportsProgress = True
bw.WorkerSupportsCancellation = True
AddHandler bw.DoWork, AddressOf bw_DoWork
AddHandler bw.ProgressChanged, AddressOf bw_ProgressChanged
AddHandler bw.RunWorkerCompleted, AddressOf bw_RunWorkerCompleted
bw.RunWorkerAsync()

Private Sub bw_DoWork()Private Sub bw_RunWorkerCompleted()Private Sub bw_ProgressChanged()

我想我需要以某种数组形式声明后台工作程序,例如变量(list/dictionary)?除此之外,我不知道该如何解决.

I think I need to declare the background workers in some sort of array like variable (list / dictionary)??? Other then that I have no idea how to tackle this.

推荐答案

尽管BackgroundWorkers有时可能是最好的,最简单和最聪明的多线程方式,但我认为您现在可能希望使用其中一种其他实现多线程的方式.

Although BackgroundWorkers can be the best, simplest, and smartest way to multithread sometimes, I think you might now look to use one of the other ways to multithread.

关于每种情况下最适合使用哪种方法,有很多辩论/争论/辩论,所以我对您的建议是快速浏览以下文章并自己决定(或者是否可以这样做).找不到足够的资源来做出决定,请问SO.)

There are lots of debates/arguments/trolling regarding which methods are the best to use in each circumstance, so my advice to you would be to have a quick look at the following articles and decide for yourself (or if you can't find good enough resources to make a decision, ask on SO of course).

您显然已经看过地面工作人员,所以我不会列出他们,也不会列出所有所有您可以穿线的方法,只是您可能感兴趣的几种方法.

You've obviously looked at back ground workers already so I won't list them, nor will I list all the ways you can thread, just a couple that might be of interest to you.

首先,查看 ThreadPool .它易于使用,并且可以很好地利用回收/再利用资源.有一些弊端,例如使用/持有过多的线程池可以耗尽该池,但是在简单应用程序中这应该不是问题.

First off, check out the ThreadPool. It's easy to use, and it makes fairly good use of recycling/re-using resources. There are some cons such as using/holding too many threads from a pool can exhuast the pool, but in simple applications that shouldn't be an issue.

还有 CLR异步模型,该框架在数量惊人的框架中受支持本身,尤其是在涉及某种形式的IO资源(文件,网络等)的情况下.

There is also the CLR Async model which is supported across a suprising amount of the framework itself, particularly in cases involving some form of IO resource (file, network, etc).

另一种方法是平行类是我的最爱之一-自从引入多行lambda以来,我就迷上了它,而并行提供了一个很好的平台.

Another approach is the Parallel Class which is one of my favourites - I've been hooked on multiline lambda since it was introduced and parallel provides a good platform for doing so.

在上述所有情况下,您都可以即时创建第三级线程,而不必自己创建和维护后台工作人员池.很难说从提供的信息中哪种方法最适合您,但是就个人而言,如果检索用于填充选项卡的数据花费的时间不长,我会考虑使用线程池.

In all of the above cases, you can create tertiary threads on the fly, without having to create and maintain a pool of background workers yourself. It's hard to say which approach would work best for you from the information provided, but personally, I'd consider the threadpool if retrieval of the data to populate your tabs doesn't take too long.

希望有帮助!

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

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