Multiple Args to Background worker

J

John Veldthuis

I have set up a background worker and now find I need to give it multiple arguments.
What is the best way of doing this? An example would be nice.
 
B

Bart Mermuys

Hi,

John Veldthuis said:
I have set up a background worker and now find I need to give it multiple
arguments.
What is the best way of doing this? An example would be nice.

I assume your talking about BackgroundWorker (NET2.0 component). Well you
can pass one argument, if you need to pass multiple then you could use a
class, a Hashtable or an array to group them and then pass that.

* Example using an array:

' start BackgroundWorker
BackgroundWorker1.RunWorkerAsync( New Object() { 10, "test" } )

' DoWork event
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e
As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

Dim args As Object() = DirectCast( e.Argument, Object() )

Dim arg1 As Integer = CInt(args(0))
Dim arg2 As String = CStr(args(1))

End Sub


HTH,
Greetings
 
J

John Veldthuis

I have set up a background worker and now find I need to give it multiple arguments.
What is the best way of doing this? An example would be nice.

Also forgot I need to return multiple results as well.
 
B

Bart Mermuys

Hi,

John Veldthuis said:
Also forgot I need to return multiple results as well.

That doesn't change much, as per my previous reply you could use an array,
Hashtable or class to group parameters and pass them as an object (for
e.Argument and e.Result ):

* Example using an array (multiple args & results):

' start BackgroundWorker
BackgroundWorker1.RunWorkerAsync( New Object() { 10, "test" } )

' DoWork event
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e
As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

Dim args As Object() = DirectCast( e.Argument, Object() )

Dim arg1 As Integer = CInt(args(0))
Dim arg2 As String = CStr(args(1))

' do something

e.Result = New Object() { -1, "test" }

End Sub

Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object,
ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles
BackgroundWorker1.RunWorkerCompleted
Dim ret As Object() = DirectCast( e.Result, Object() )

Dim ret1 As Integer = CInt(ret(0))
Dim ret2 As String = CStr(ret(1))
End Sub

HTH,
Greetings
 
J

John Veldthuis

Hi,



I assume your talking about BackgroundWorker (NET2.0 component). Well you
can pass one argument, if you need to pass multiple then you could use a
class, a Hashtable or an array to group them and then pass that.

* Example using an array:

' start BackgroundWorker
BackgroundWorker1.RunWorkerAsync( New Object() { 10, "test" } )

' DoWork event
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e
As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

Dim args As Object() = DirectCast( e.Argument, Object() )

Dim arg1 As Integer = CInt(args(0))
Dim arg2 As String = CStr(args(1))

End Sub

Thanks, Just what the doctor ordered for part one.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top