View Single Post
  #6 (permalink)  
Old April 12th, 2007, 07:50 PM
planoie's Avatar
planoie planoie is offline
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

For a beginner, you have a good grasp of what's going on.

I just found this article that explains in much greater detail the specifics of how all this works:

http://www.codeproject.com/csharp/begininvoke.asp

In your step # 5, you have the exact right idea. The "worker" thread has to tell the form thread to invoke the form control update method.

To refresh my own memory I whipped up a sample app. I'll post the relevant part of the code. Hopeful it will get you going in the right direction. Here is the important parts of a sample winform app:
Code:
    Private _objThread As Thread

    Private Sub cmdStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStart.Click
        cmdStart.Enabled = False
        _objThread = New Thread(AddressOf ContinuousProcess)
        _objThread.Start()
        cmdStop.Enabled = True
    End Sub

    Private Sub cmdStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStop.Click
        cmdStop.Enabled = False
        _objThread.Abort()
        _objThread = Nothing
        cmdStart.Enabled = True
    End Sub

    Private Sub ContinuousProcess()
        Dim i As Integer = 0
        While True
            i += 1
            Me.BeginInvoke(New CounterUpdateDelegate(AddressOf UpdateCounter1), New Object() {i})
            Me.BeginInvoke(New CounterUpdateDelegate(AddressOf UpdateCounter2), New Object() {i})
            Thread.Sleep(1000)
        End While
    End Sub

    Delegate Sub CounterUpdateDelegate(ByVal count As Integer)

    Private Sub UpdateCounter1(ByVal count As Integer)
        txtCount1.Text = count.ToString
    End Sub
    Private Sub UpdateCounter2(ByVal count As Integer)
        txtCount2.Text = count.ToString
    End Sub
Delegates can be tricky to understand at first, hope this clarifies it. Think of a delegate statement in the code as a definition of a method signature. In my example:

Delegate Sub CounterUpdateDelegate(ByVal count As Integer)

defines "CounterUpdateDelegate" as a method that will take an integer and return nothing. I can have many methods that conform to that signature definition. In the example above, I intentionally created two to illustrate this point.

When I want to use one of the methods that conform to my delegate type, I create an instance of the delegate type defined ("CounterUpdateDelegate") and put into it the address of the method I wish to actually use. This is what is known as a Call Back. In this case, the logic inside of BeginInvoke is going to "Call Back" to the method I handed it through the callback delegate.

-Peter
Reply With Quote