How to dynamically create a background worker in VB.net

后端 未结 3 1666
谎友^
谎友^ 2020-12-18 17:32

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

A user can enter

相关标签:
3条回答
  • 2020-12-18 17:46

    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 sender argument in each handler can be used to check which worker is reporting progress etc..

    0 讨论(0)
  • 2020-12-18 17:51

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

    Friend WithEvents bgw1 As BackgroundWorker
    

    then in class constructor instantiate it.

    Public Sub New()
      bgw1 = New BackgroundWorker
    End Sub
    

    In class level declaration choose bgw1 and its event dropdown section choose DoWork, ProgressChanged and RunWorkerCompleted Events.

    0 讨论(0)
  • 2020-12-18 17:54

    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.

    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.

    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.

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

    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.

    Hope that helps!

    0 讨论(0)
提交回复
热议问题