vb.net 如何在VB.net中动态创建后台工作者

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6376972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:33:05  来源:igfitidea点击:

How to dynamically create a background worker in VB.net

vb.netbackgroundworker

提问by PeeHaa

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

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

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

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

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.

用户可以在文本框中输入 URL,当用户单击解析按钮时,程序会创建一个新的选项卡控件并输出一些数据。

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()and Private Sub bw_ProgressChanged()

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

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.

我想我需要在某种数组中声明后台工作人员,比如变量(list/ dictionary)???除此之外,我不知道如何解决这个问题。

回答by P?l Thingb?

Here is how

这是如何

Public Class Form
    Private Workers() As BackgroundWorker
    Private NumWorkers = 0

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        NumWorkers = NumWorkers + 1
        ReDim Workers(NumWorkers)
        Workers(NumWorkers) = New BackgroundWorker
        Workers(NumWorkers).WorkerReportsProgress = True
        Workers(NumWorkers).WorkerSupportsCancellation = True
        AddHandler Workers(NumWorkers).DoWork, AddressOf WorkerDoWork
        AddHandler Workers(NumWorkers).ProgressChanged, AddressOf WorkerProgressChanged
        AddHandler Workers(NumWorkers).RunWorkerCompleted, AddressOf WorkerCompleted
        Workers(NumWorkers).RunWorkerAsync()
    End Sub

End Class

Then the handlers

然后处理程序

Private Sub WorkerDoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs)
    ' Do some work
End Sub

Private Sub WorkerProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs)
    ' I did something!
End Sub

Private Sub WorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs)
    ' I'm done!
End Sub

Imagine multithreading could be so easy. This works great unless you have 1000's of workers. The senderargument in each handler can be used to check which worker is reporting progress etc..

想象一下,多线程可以如此简单。除非你有 1000 名工人,否则这很好用。每个处理程序中的sender参数可用于检查哪个工作人员正在报告进度等。

回答by Smudge202

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.

尽管 BackgroundWorkers有时可能是多线程的最佳、最简单和最聪明的方法,但我认为您现在可能会考虑使用其他方法之一进行多线程。

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).

关于在每种情况下最好使用哪种方法有很多争论/争论/拖钓,所以我给你的建议是快速浏览以下文章并自己决定(或者如果你找不到好的足够的资源来做出决定,当然要问 SO)。

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

您显然已经看过后台工作人员,所以我不会列出他们,也不会列出您可以使用的所有方式,只列出您可能感兴趣的几个。

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 simpleapplications that shouldn't be an issue.

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

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

还有CLR 异步模型,它支持大量框架本身,特别是在涉及某种形式的 IO 资源(文件、网络等)的情况下。

Another approach is the Parallel Classwhich 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.

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

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.

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

Hope that helps!

希望有帮助!

回答by peyman

If I understand your question well, try declaring a BackgroundWorkerin class level as:

如果我很好地理解您的问题,请尝试将BackgroundWorker课堂级别声明为:

Friend WithEvents bgw1 As BackgroundWorker

then in class constructor instantiate it.

然后在类构造函数中实例化它。

Public Sub New()
  bgw1 = New BackgroundWorker
End Sub

In class level declaration choose bgw1and its event dropdown section choose DoWork, ProgressChangedand RunWorkerCompletedEvents.

在类级别宣言选择bgw1和它的事件下拉部分选择DoWorkProgressChangedRunWorkerCompleted活动。