INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

HANDLE


PASSWORD
Remember Me
Forgot Password?

Come Join Us!

  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • Turn Off Ad Banners
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

E-mail*
Handle

Password
Verify P'word
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Member Feedback

"...Keep up the good work - excellent site - i'd been looking for something like this for ages !..."

Geography

Where in the world do Tek-Tips members come from?

Visual Basic(Microsoft) -VB.NET 2002-2008 FAQ

How-to

An Intro to Threading 3: Crossing thread boundries (and passing parameters)
Posted: 15 Sep 05 (Edited 21 Sep 06)

Another chunk of threading code today. One of the primary reasons for moving a process to a different thread is to get the load off of the primary thread. That leaves the primary thread free to refresh and paint the form while your other process works in another thread. But what happens when you need to do something on that primary thread from the new thread?

In previous examples we've been updating the text of a label. And while you can do this in Framework 1.0 and 1.1, you can't in 2.0 (note, I haven't tried this code in 2.0). And Microsoft frowns apon it. And when you try to work with more advanced objects, race conditions can occur (as I found out with a common dialog box earlier this week).

So, here is a block of code that will let you call a 'DisplayMessage' function on the form from the primary thread. When you call this method from another thread it creates a delegate and starts the call on the forms thread. I also threw in the parameter passing since this is a fairly straight forward sample.

This code sample should work in .Net v1.0, 1.1 and 2.0, but I have not tested it in v2.0.

CODE

'The public method on your form
'that you can call from any thread

Public Sub DisplayMessage(ByVal Message As string)
  'check if the call is coming in
  'on a thread other then the one this oject
  'was created in

  If Me.InvokeRequired Then
    'Call is from a different thread
    'create a delegate to call the method

    'Use an object array to store the parameters

    Dim obj(0) As Object
    obj(0) = Message
    'Use Begin Invoke to call this
    'method on this instance's thread

    Me.BeginInvoke(New m_delDisplayMessage(AddressOf DisplayMessage), obj)
  Else
    'Call is from the same thread,
    'just set the text

    Me.StatusBar.Panels(0).Text = Message
  End If
End Sub

'The Delegate definition for our method
Private Delegate Sub m_delDisplayMessage(ByVal Message as String)

-Rick

Back to Visual Basic(Microsoft) -VB.NET 2002-2008 FAQ Index
Back to Visual Basic(Microsoft) -VB.NET 2002-2008 Forum
My FAQ Archive
Email This FAQ To A Friend

My Archive