Beruflich Dokumente
Kultur Dokumente
com/DevX/10MinuteSolution/20365/1954
Since VB.NET runs with the Comm on Language Runtim e (CLR), it gains many new
capabilities, one of which is the ability to create free- threaded applications.
by Matthew Arnheiter
evelopers have been requesting that Microsoft add more threading functionality to VB for some time—it
will finally happen with VB.NET. VB6 does have support for building multithreaded EXEs, DLLs, and OCXs.
But, the wording for this is somewhat misleading, in that VB6 supports running multiple single-threaded
apartments. An apartment is really just a room in which code is executed and the boundaries of the
apartment restrict the code from accessing anything outside of it.
VB.NET natively supports building free-threaded applications. This means that multiple threads can access
the same set of shared data. The following article will walk you through the basics of multithreading.
While VB6 supports multiple single-threaded apartments, it does not support a free-threading model, which
allows multiple threads to run against the same set of data. There are many situations in which spawning a
new thread to run a background process would increase the usability of your application. This situation is
evident when you want to place a cancel button on a form where a long process can make a form seem
unresponsive.
Since VB.NET runs with the Common Language Runtime (CLR), it gains many new capabilities, one of
which is the ability to create free-threaded applications.
Do While True
ListBox1.Items.Add("Iterations: " + i)
i += 1
Loop
End Sub
This code loops infinitely and adds an item to a listbox on a form for each iteration. As a side note, if you are
not familiar with VB.NET then you may have noticed a few other things in this code that you could not do in
VB6:
Once we have a worker process, we need to assign this block of code to a new thread and start its
execution. To do this we use the Thread object that is part of the System.Threading namespace in the .NET
framework classes. When we instantiate a new Thread class we pass it a reference to the code block we
want to execute in the constructor of the Thread class. The following code creates a new Thread object and
passes it a reference to BackgroundProcess:
Dim t As Thread
t = New Thread(AddressOf Me.BackgroundProcess)
t.Start()
The AddressOf operator creates a delegate object to the BackgroundProcess method. A delegate within
VB.NET is a type-safe, object-oriented function pointer. After the thread has been instantiated, you begin the
execution of the code by calling the Start() method of the thread.
Do While True
ListBox1.Items.Add("Iterations: " + i)
i += 1
Thread.CurrentThread.Sleep(2000)
Loop
End Sub
CurrentThread is a public static property that allows you to retrieve a reference to the currently running thread.
You can also place a thread into the sleep state for an indeterminate amount of time by calling Thread.Sleep
(System.Threading.Timeout.Infinite). To interrupt this sleep you can call the Thread.Interrupt method.
Similar to Sleep and Interrupt are Suspend and Resume. Suspend allows you to block a thread until another
thread calls Thread.Resume. The difference between Sleep and Suspend is that the latter does not
immediately place a thread in the wait state. The thread does not suspend until the .NET runtime determines
that it is in a safe place to suspend it. Sleep will immediately place a thread in a wait state.
Lastly, Thread.Abort stops a thread from executing. In our simple example, we would want to add another
button on the form that allows us to stop the process. To do this all we would have to do is call the
Thread.Abort method as follows:
This is where the power of multithreading can be seen. The UI seems responsive to the user because it is
running in one thread and the background process is running in another thread. The cancel button
immediately responds to the user's click event and processing stops.
A simple example of this would be if we had a procedure that calculated the square of a number:
To make this procedure available to be used in a new thread we would wrap it in a class:
Use this code to start the CalcSquare procedure on a new thread. following code:
oSquare.Value = 30
t.Start()
End Sub
Notice that after the thread is started, we do not inspect the square value of the class, because it is not
guaranteed to have executed once you call the start method of the thread. There are a few ways to retrieve
values back from another thread. The easiest way is to raise an event when the thread is complete. We will
examine another method in the next section on thread synchronization. The following code adds the event
declarations to the SquareClass.
Public Class SquareClass
Public Value As Double
Public Square As Double
Catching the events in the calling code has not changed much from VB6, you still declare the variables
WithEvents and handle the event in a procedure. The part that has changed is that you declare that a
procedure handles the event using the Handles keyword and not through the naming convention of
Object_Event as in VB6.
oSquare.Value = 30
t.Start()
End Sub
End Sub
The one thing to note with this method is that the procedure handling the event, in this case
SquareEventHandler, will run within the thread that raised the event. It does not run within the thread from
which the form is executing.
SyncLock gains an exclusive lock to an object reference that is passed to it. By gaining this exclusive lock you
can ensure that multiple threads are not accessing shared data or that the code is executing on multiple
threads. A convenient object to use in order to gain a lock is the System.Type object associated with each
class. The System.Type object can be retrieved using the GetType method:
Lastly, the Thread.Join method allows you to wait for a specific amount of time until a thread has completed.
If the thread completes before the timeout that you specify, Thread.Join returns True, otherwise it returns
False. In the square sample, if we did not want to raise events, we could call the Thread.Join method to
determine if the calculation has finished. The code would look like the following:
oSquare.Value = 30
t.Start()
If t.Join(500) Then
MsgBox(oSquare.Square)
End If
End Sub
The one thing to note with this method is that the procedure handling the event, in this case
SquareEventHandler, will run within the thread that raised the event. It does not run within the thread from
which the form is executing.