With Visual Basic .NET, you can write applications that perform multiple tasks simultaneously. Tasks with the potential of holding up other tasks can execute on separate threads, a process known as multithreading or free threading. Applications that use multithreading are more responsive to user input because the user interface stays active while processor-intensive tasks execute on separate threads. Multithreading is also useful when creating scalable applications, because you can add threads as the workload increases.
Creating and Using Threads
You create a new thread in Visual Basic .NET by declaring a variable of type System.Threading.Thread and calling the constructor with the AddressOf statement and the name of the procedure or method you want to execute on the new thread. The following code provides an example:
Dim MyThread As New System.Threading.Thread(AddressOf MySub)
Starting and Stopping Threads
To start the execution of a new thread, use the Start method, as in the following code:
To stop the execution of a thread, use the Abort method, as in the following code:
Besides starting and stopping threads, you can also pause threads by calling the Sleep or Suspend methods, resume a suspended thread with the Resume method, and destroy a thread using the Abort method, as in the following code:
MyThread.Sleep () MyThread.Suspend () MyThread.Abort ()
The following table shows some of the methods you can use to control individual threads.
|Start||Causes a thread to start running.|
|Sleep||Pauses a thread for a specified time.|
|Suspend||Pauses a thread when it reaches a safe point.|
|Abort||Stops a thread when it reaches a safe point.|
|Resume||Restarts a suspended thread|
|Join||Causes the current thread to wait for another thread to finish. If used with a timeout value, this method returns True if the thread finishes in the allotted time.|
Most of these methods are self explanatory, but the concept of safe points may be new to you. Safe points are places in code where it is safe for the common language runtime to perform automatic garbage collection, the process of releasing unused variables and reclaiming memory. When you call the Abort or Suspend methods of a thread, the common language runtime analyzes the code and determines the location of an appropriate place for the thread to stop running.
Threads also contain a number of useful properties, as shown in the following table:
|thread.IsAlive||Contains the value True if a thread is active.|
|IsBackground||Gets or sets a Boolean that indicates if a thread is or should be a background thread. Background threads are like foreground threads, but background threads do not prevent a process from terminating. Once all foreground threads belonging to a process have terminated, the common language runtime ends the process by calling the Abort method on background threads that are still alive.|
|Name||Gets or sets the name of a thread. Most commonly used to discover individual threads when debugging.|
|Priority||Gets or sets a value used by the operating system to prioritize thread scheduling.|
|ApartmentState||Gets or sets the threading model used for a particular thread. Threading models are important when a thread calls unmanaged code.|
|ThreadState||Contains a value that describes a thread's state or states.|
See Thread States for more information on thread states and methods.
Every thread has a priority property — that determines how large of a time slice it gets to execute. The operating system allocates longer time slices to high-priority threads than it does to low-priority threads. New threads are created with the value of Normal, but you can adjust the Priority property to any of the other values in the System.Threading.ThreadPriority enumeration.
See ThreadPriority Enumeration for a detailed description of the various thread priorities.
Foreground and Background Threads
A foreground thread runs indefinitely, while a background thread terminates once the last foreground thread has stopped. You can use the IsBackground property to determine or change the background status of a thread.
Thread States | Parameters and Return Values for Multithreaded Procedures | Thread Synchronization | Multithreading with Forms and Controls | Multithreading in Components