user3680973
user3680973

Reputation: 21

Re: How to dynamically create a background worker in vb net

I have a follow-on question about this question:

How to dynamically create a background worker in VB.net

I'm afraid I don't understand how DirectCast would solve my problem. I am creating an array of backgroundworkers: bWorker(0), bWorker (1), etc. each one creates a new form which is also in an array: page(0), page(1) etc.

The background workers need to 'know' their index number so that they can create the appropriate page (which is a form). As I mentioned before, I have found a system that works. I put the backgroundworkers' hashcodes in an array which I use to retrieve their index numbers. It just feels a bit clunky and maybe using DirectCast would be better but I don't understand how.

Upvotes: 1

Views: 2244

Answers (2)

user3680973
user3680973

Reputation: 21

I have found the solution. The problem I had with the e.Argument system was that it only applied to DoWork. I use RunWorkerCompleted to show the form created (if I try to show it with DoWork things go badly wrong). RunWorkerCompleted has no e.Argument property. My solution is to do this: e.Result = e.Argument in DoWork because e.Result is available in RunWorkerCompleted.

Upvotes: 0

user3453226
user3453226

Reputation:

BackgroundWorker.RunWorkerAsync has two overloads. See BackgroundWorker.RunWorkerAsync(Object).

Now you can pass a variable and get it on BackgroundWorker.DoWork using DoWorkEventArgs.Argument.

Dim BackgroundWorker1 As New System.ComponentModel.BackgroundWorker
AddHandler BackgroundWorker1.DoWork, AddressOf BackgroundWorker_DoWork
Dim BackgroundWorker2 As New System.ComponentModel.BackgroundWorker
AddHandler BackgroundWorker2.DoWork, AddressOf BackgroundWorker_DoWork
' I'm going to pass integers, but you can pass whatever you want.
BackgroundWorker1.RunWorkerAsync(0)
BackgroundWorker2.RunWorkerAsync(1)

Private Sub BackgroundWorker_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs)
    Select Case e.Argument
        Case 0
            ' Form1
        Case 1
            ' Form2
    End Select
End Sub

Upvotes: 1

Related Questions