Reputation: 881
i am writing a program with background worker , i am creating it dynamically but how can i add doWork code inside each background worker in vb.net
For NumWorkers As Integer = 0 To 3
NumWorkers = NumWorkers + 1
ReDim Workers(NumWorkers)
Workers(NumWorkers) = New BackgroundWorker
Workers(NumWorkers).WorkerReportsProgress = True
Workers(NumWorkers).WorkerSupportsCancellation = True
AddHandler Workers(NumWorkers).DoWork, AddressOf WorkerDoWork
Workers(NumWorkers).RunWorkerAsync()
Next
Private Sub WorkerDoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs)
' for first
End SUb
Private Sub WorkerDoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs)
' for 2nd
End SUb
Private Sub WorkerDoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs)
' for 3rd
End SUb
Private Sub WorkerDoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs)
' for 4th
End SUb
i am confussing about last 3 workerDork() . how can i add the codes in it ??
Upvotes: 2
Views: 2478
Reputation:
here an example of what you're trying to accomplish
Imports System.ComponentModel
Module Module1
Sub Main()
StartSomeThread(
AddressOf Worker1DoWork,
AddressOf Worker2DoWork,
AddressOf Worker3DoWork,
AddressOf Worker4DoWork)
End Sub
Private Sub Worker1DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
End Sub
Private Sub Worker2DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
End Sub
Private Sub Worker3DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
End Sub
Private Sub Worker4DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
End Sub
Private workers As New List(Of BackgroundWorker)
Public Sub StartSomeThread(ParamArray jobs As Action(Of Object, DoWorkEventArgs)())
For Each job In jobs
Dim worker = New BackgroundWorker
worker.WorkerReportsProgress = True
worker.WorkerSupportsCancellation = True
AddHandler worker.DoWork, AddressOf job.Invoke
workers.Add(worker)
worker.RunWorkerAsync()
Next
End Sub
End Module
Upvotes: 2
Reputation: 141
you Need to add handler and with different address of each Background worker
so you have to create each worker dynamically add different address for them .
Upvotes: 2