Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

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

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

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

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 FAQ

How-to

An Intro to Threading 2: Delegates
Posted: 19 Aug 05 (Edited 15 Sep 05)

In the first FAQ on Multithreading (FAQ796-5929) we went over a basic method of launching a thread. That method was fire and forget, and we couldn't pass any values. Delegates give us a lot more power in that we can pass in parameters and use call back methods. A Call Back is a sub that is called from when the new thread ends, it is run on the calling thread which means that we can access the GUI safely from it if the thread was launched from the GUI.

CODE

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
   'first, disable the button1 object
   'so the user can't click it again

   Button1.Enabled = False
   del = New ProcessDelegate(AddressOf Me.Process)
   Dim cb As New AsyncCallback(AddressOf Me.ProcessComplete)
   del.BeginInvoke(cb, del)
End Sub

'The Delegate definition
Protected Delegate Sub ProcessDelegate()

'The delegate that will launch the
'process

Protected del As ProcessDelegate

'The Callback method the will launch
'in the original thread when the thread
'completes

Protected Sub ProcessComplete(ByVal ar As System.IAsyncResult)
  me.button1.enabled = true
end sub

'The long process
Protected Sub Process()
   For a As Integer = 1 To 10
      For b As Integer = 1 To 100000000
         'do something really
         'important here

      Next
      'simulate a progress bar
      '(This stuff should be handled in a
      'thread safe manor, but this will
      'work pre-2k5)

      Label2.Text = a.ToString
      Label2.Refresh()
   Next
End Sub

-Rick

Back to Visual Basic(Microsoft) -VB.NET FAQ Index
Back to Visual Basic(Microsoft) -VB.NET Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close