Multi-Threading and Delegates Tutorial in VB .NET

xster
xster
Published in
2 min readOct 24, 2009

This guide will show you everything you need to create a non-GUI multi-threaded application in VB .NET.

There are 2 ways of using multi-threading in VB .NET. First by making use of delegates which I will explain here. And second by programming the threads manually (not actually more complicated).

Using delegates, .NET helps simplify the entire process of using a separate thread and saves you from having to manage the parameter passing, result retrieving and timing issues.

First to simply call a method on a separate thread with a parameter

Delegate Function MyDelegate(ByVal text as String) as StringFunction PrintStuff(ByVal text as String) as String
Console.WriteLine(text)
Thread.Sleep(5000)
Return "Success"
End Function
Sub Main()
Dim d as MyDelegate = AddressOf PrintStuff
d.BeginInvoke("hello world", Nothing, Nothing)
'I can do more stuff here
End Sub
Here we have a Main thread and a second thread to print some stuff. Also, a delegate with the same signature as the function we want to run (PrintStuff) already exists. Very simply, the Main thread creates a delegate that points to PrintsStuff and calls for the delegate to start executing PrintStuff on a separate thread. A parameter is passed to PrintStuff in the process and Main can continue working while PrintStuff is busy.
Now suppose you want to know when PrintStuff finishes because you want the result "Success". We can do this by the use of a callback function.First, we create the callback functionSub MyCallback(ByVal result as IAsyncResult)
Console.WriteLine("Now I know PrintStuff finished")
End Sub
Then we make sure the callback function gets called by changing the previous BeginInvoke command tod.BeginInvoke("hello world", New AsyncCallback(AddressOf MyCallback), Nothing)In other words, the before last parameter (depending on how many parameters your MyDelegate is supposed to take, since they come in front) is where you specify your callback function. Now MyCallback will be called when PrintStuff is done (ie after about 5 seconds)Now we need MyCallback function to actually read the return of PrintStuff ("Success"). It is contained in the IAsyncResult.Sub MyCallback(ByVal result as IAsyncResult)
Console.WriteLine("Now I know PrintStuff finished")
Dim resultClass = CType(result, AsyncResult)
Dim d as MyDelegate = CType(resultClass.AsyncDelegate, MyDelegate)
Console.WriteLine("And I also know that the result is: " & d.EndInvoke(result))
End Sub
In other words, (instance of MyDelegate).EndInvoke will give you the return of PrintStuff. You could have simply called it in your Main instead of MyCallback, but then Main will remain on that line until PrintStuff reaches its Return line, defeating the whole purpose of multi-threading. When you use EndInvoke in the callback function, you are guaranteed that PrintStuff has already reached its Return line and you simply have to retrieve the result.For multi-threading applications involving GUI elements, check by other tutorial to see how to avoid illegal cross-thread operations.

--

--