Joining Threads

106 6


If you reached this page using a search for threading, you might want to check out all of the articles here on that topic. A menu of all articles is available in the top of the first article of the series:

Introduction to Threading and VB.NET

-------

Like Start (discussed in the introductory article), Join is a method of the Threading.Thread object. But if you read the Microsoft documentation for Join, you might not get a very clear idea about exactly what it does:

Join
Blocks the calling thread until a thread terminates.

When I read this, my first thought was, "That's all there is? Join simply blocks something else?"

It makes sense after you work with it. Let's do that now.

To begin, let's code a simple thread that does something slightly more interesting than just Console.WriteLine. Since Windows Form applications force single threaded apartment threading, this example is a Console application again. The code starts up a new thread, then Notepad is started in the new thread:

Module Module1Class MainClassPublic Shared Sub Main()Dim NotePadThread As New Threading.Thread(AddressOf Work.DoNotePad)NotePadThread.Start()Debug.Print(Now.ToLongTimeString &" From Sub Main after NotePadThread.Start")End SubEnd ClassPublic Class WorkShared Sub DoNotePad()Shell("Notepad.exe",vbNormalFocus, True)... or ...Process.Start("Notepad.exe")Debug.Print(Now.ToLongTimeString & " From Sub DoNotePad")End SubEnd ClassEnd Module

This works pretty well. The third "True" parameter of the Shell function simulates waiting for the program to complete doing something. (Not everything will work this way. For example, IE (iexplore.exe), will return control to the calling thread even when the Wait parameter is true.) Control isn't returned to the program until NotePad is closed which simulates the Main thread needing some information provided by the new thread before continuing. The problem, however, is that because these threads are now independent, the Main thread goes ahead anyway. The result looks like this:

12:39:41 PM From Sub Main after NotePadThread.Start12:39:45 PM From Sub DoNotePad
If Sub Main needed some information from DoNotePad or was waiting for some process there to complete, the program wouldn't work because Sub DoNotePad hadn't finished yet. Adding a Threading.Thread.Sleep statement doesn't help. In general, you won't be able to guess how long the Main thread should wait, and you don't want to compromise performance by waiting longer than necessary.

The solution is to Join the threads. Here's the critical statements in the Main thread with a Join added:

Dim NotePadThread As New Threading.Thread(AddressOf Work.DoNotePad)NotePadThread.Start()NotePadThread.Join() ' Wait for NotePadThread to finish.
Now, we get a result like this:

12:50:19 PM From Sub DoNotePad12:50:19 PM From Sub Main after NotePadThread.Start with Join!
Sub Main doesn't finish until after the Shelled program completes.

You can do this trick on as many new threads as you like. Here's the code for two new threads:

Public Shared Sub Main()Dim NotePadThread1 As New Threading.Thread(AddressOf DoNotePad)Dim NotePadThread2 As New Threading.Thread(AddressOf DoNotePad)NotePadThread1.Start()NotePadThread2.Start()NotePadThread1.Join() ' Wait for NotePadThread to finish.NotePadThread2.Join() ' Wait for NotePadThread to finish.Debug.Print(Now.ToLongTimeString &" From Sub Main after Two Threads" &" with Join on both!")End Sub
And the result:

4:01:34 PM DoNotePad4:01:37 PM DoNotePad4:01:37 PM From Sub Main after Two Threads with Join on both!
Note that the Main thread doesn't start up again until both threads have completed. Solving this problem is generally called interprocess communication. But there's a limit to what you can do with Start and Join because you can't notify the Main thread that an event has occurred in the new thread; all you can do is wait for the new thread to complete. This next level in threading requires some different objects. The next article in this series, Threading - Locks: InterLock and SyncLock covers these new objects. covers some of the objects that will solve the problem.
Source...
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.