Home All Groups Group Topic Archive Search About

Paramters passing and RunWorkerCompleted event

Author
22 Jun 2006 6:27 PM
nt8jbwu02
I am trying to use the example at:
http://msdn2.microsoft.com/en-us/library/ms171728(d=ide).aspx
to update a control in a thread safe maner.

The article shows a couple of ways to do so and indicates using the
RunWorkerCompleted() event handler is the preferred.  However, the
example they have does not show how to pass a parameter to use, but
instead uses a hardcoded value:

' BackgroundWorker is the preferred way to perform asynchronous
' operations.
Private Sub backgroundWorker1_RunWorkerCompleted( _
ByVal sender As Object, _
ByVal e As RunWorkerCompletedEventArgs) _
Handles backgroundWorker1.RunWorkerCompleted
     Me.textBox1.Text = _
     "This text was set safely by BackgroundWorker."
End Sub

How can I use this method with a passed in value?

Thanks.

Author
22 Jun 2006 10:42 PM
gene kelley
On 22 Jun 2006 11:27:10 -0700, nt8jbw***@sneakemail.com wrote:

Show quoteHide quote
>I am trying to use the example at:
>http://msdn2.microsoft.com/en-us/library/ms171728(d=ide).aspx
>to update a control in a thread safe maner.
>
>The article shows a couple of ways to do so and indicates using the
>RunWorkerCompleted() event handler is the preferred.  However, the
>example they have does not show how to pass a parameter to use, but
>instead uses a hardcoded value:
>
>' BackgroundWorker is the preferred way to perform asynchronous
>' operations.
> Private Sub backgroundWorker1_RunWorkerCompleted( _
> ByVal sender As Object, _
> ByVal e As RunWorkerCompletedEventArgs) _
> Handles backgroundWorker1.RunWorkerCompleted
>     Me.textBox1.Text = _
>     "This text was set safely by BackgroundWorker."
> End Sub
>
>How can I use this method with a passed in value?
>
>Thanks.

I think you are misreading the example.  The example demo's unsafe and
safe methods.  The code you included is only a piece of the safe
method which is as advertised - RunWorkerComplete:

You need all of this:

' This event handler creates a thread that calls a
   ' Windows Forms control in a thread-safe way.
    Private Sub setTextSafeBtn_Click( _
    ByVal sender As Object, _
    ByVal e As EventArgs) Handles setTextSafeBtn.Click

        Me.demoThread = New Thread( _
        New ThreadStart(AddressOf Me.ThreadProcSafe))

        Me.demoThread.Start()
    End Sub


   ' This method is executed on the worker thread and makes
   ' a thread-safe call on the TextBox control.
   Private Sub ThreadProcSafe()
      Me.SetText("This text was set safely.")
    End Sub

   ' This method demonstrates a pattern for making thread-safe
   ' calls on a Windows Forms control.
   '
   ' If the calling thread is different from the thread that
   ' created the TextBox control, this method creates a
   ' SetTextCallback and calls itself asynchronously using the
   ' Invoke method.
   '
   ' If the calling thread is the same as the thread that created
    ' the TextBox control, the Text property is set directly.

    Private Sub SetText(ByVal [text] As String)

        ' InvokeRequired required compares the thread ID of the
        ' calling thread to the thread ID of the creating thread.
        ' If these threads are different, it returns true.
        If Me.textBox1.InvokeRequired Then
            Dim d As New SetTextCallback(AddressOf SetText)
            Me.Invoke(d, New Object() {[text]})
        Else
            Me.textBox1.Text = [text]
        End If
    End Sub

   ' This event handler starts the form's
   ' BackgroundWorker by calling RunWorkerAsync.
   '
   ' The Text property of the TextBox control is set
   ' when the BackgroundWorker raises the RunWorkerCompleted
   ' event.
    Private Sub setTextBackgroundWorkerBtn_Click( _
    ByVal sender As Object, _
    ByVal e As EventArgs) Handles setTextBackgroundWorkerBtn.Click
        Me.backgroundWorker1.RunWorkerAsync()
    End Sub


   ' This event handler sets the Text property of the TextBox
   ' control. It is called on the thread that created the
   ' TextBox control, so the call is thread-safe.
   '
   ' BackgroundWorker is the preferred way to perform asynchronous
   ' operations.
    Private Sub backgroundWorker1_RunWorkerCompleted( _
    ByVal sender As Object, _
    ByVal e As RunWorkerCompletedEventArgs) _
    Handles backgroundWorker1.RunWorkerCompleted
        Me.textBox1.Text = _
        "This text was set safely by BackgroundWorker."
    End Sub


Gene
Author
23 Jun 2006 1:54 PM
nt8jbwu02
gene kelley wrote:
Show quoteHide quote
> On 22 Jun 2006 11:27:10 -0700, nt8jbw***@sneakemail.com wrote:
>
> >I am trying to use the example at:
> >http://msdn2.microsoft.com/en-us/library/ms171728(d=ide).aspx
> >to update a control in a thread safe manner.
> >
> >The article shows a couple of ways to do so and indicates using the
> >RunWorkerCompleted() event handler is the preferred.  However, the
> >example they have does not show how to pass a parameter to use, but
> >instead uses a hardcoded value:
> >
> >' BackgroundWorker is the preferred way to perform asynchronous
> >' operations.
> > Private Sub backgroundWorker1_RunWorkerCompleted( _
> > ByVal sender As Object, _
> > ByVal e As RunWorkerCompletedEventArgs) _
> > Handles backgroundWorker1.RunWorkerCompleted
> >     Me.textBox1.Text = _
> >     "This text was set safely by BackgroundWorker."
> > End Sub
> >
> >How can I use this method with a passed in value?
> >
> >Thanks.
>
> I think you are misreading the example.  The example demo's unsafe and
> safe methods.  The code you included is only a piece of the safe
> method which is as advertised - RunWorkerComplete:
>
>
Gene,

I think you missed my question.  Yes I know you need all of the code, I
was simply pointing to the code I had a question about.

Let me try and restate it since it was not obvious.

I would like to pass a variable parameter to the routine that is going
to update the control.  In the example, there is no way to do this as
the value going to the control is static.  ie:"This text was set safely
by BackgroundWorker."

My question is how can I use this preferred thread safe method and pass
in the value to be sent to the control?

Thanks.
Author
23 Jun 2006 3:17 PM
Claes Bergefall
> I would like to pass a variable parameter to the routine that is going
> to update the control.  In the example, there is no way to do this as
> the value going to the control is static.  ie:"This text was set safely
> by BackgroundWorker."
>
> My question is how can I use this preferred thread safe method and pass
> in the value to be sent to the control?

In DoWork event handler, set the Result property in the passed in
DoWorkEventArgs. You can then get that value from the Result property of the
RunWorkerCompletedEventArgs in the RunWorkerCompleted event handler. You can
also pass a parameter to DoWork by using the overload of RunWorkerAsync and
then use the Argument property of the DoWorkEventArgs to access it.


worker.RunWorkerAsync("My Argument")
.....
Private Sub worker_DoWork(ByVal sender As System.Object, ByVal e As
System.ComponentModel.DoWorkEventArgs) Handles worker.DoWork
    Debug.WriteLine(e.Argument.ToString())
    e.Result = "My Result"
End Sub

Private Sub worker_RunWorkerCompleted(ByVal sender As System.Object, ByVal e
As System.ComponentModel.RunWorkerCompletedEventArgs) Handles
worker.RunWorkerCompleted
    Debug.WriteLine(e.Result.ToString())
End Sub


   /claes