Bagikan melalui


Multithreaded Applications

With Visual Basic, you can write applications that perform multiple tasks at the same time. 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 as processor-intensive tasks execute on separate threads. Multithreading is also useful when you create scalable applications, because you can add threads as the workload increases.

Using the BackgroundWorker Component

The most reliable way to create a multithreaded application is to use the BackgroundWorker component. This class manages a separate thread dedicated to processing the method that you specify. For an example, see Walkthrough: Multithreading.

To start an operation in the background, create a BackgroundWorker and listen for events that report the progress of your operation and signal when your operation is finished. You can create the BackgroundWorker object programmatically, or you can drag it onto a form from the Components tab of the Toolbox. If you create the BackgroundWorker in the Forms Designer, it appears in the Component Tray, and its properties are displayed in the Properties window.

Setting Up for a Background Operation

To set up for a background operation, add an event handler for the DoWork event. Call your time-consuming operation in this event handler.

To start the operation, call RunWorkerAsync. To receive notifications of progress updates, handle the ProgressChanged event. To receive a notification when the operation is completed, handle the RunWorkerCompleted event.

The methods that handle the ProgressChanged and RunWorkerCompleted events can access the application's user interface, because those events are raised on the thread that called the RunWorkerAsync method. However, the DoWork event handler cannot work with any user-interface objects because it runs on the background thread.

Creating and Using Threads

If you need more control over the behavior of your application's threads, you can manage the threads yourself. However, realize that writing correct multithreaded applications can be difficult: Your application may stop responding or experience transient errors caused by race conditions. For more information, see Thread-Safe Components.

You create a new thread in Visual Basic by declaring a variable of type Thread and calling the constructor with the AddressOf statement and the name of the procedure or method that you want to execute on the new thread. The following code provides an example:

Dim TestThread As New System.Threading.Thread(AddressOf TestSub)

Starting and Stopping Threads

To start the execution of a new thread, use the Start method, as in the following code:

TestThread.Start()

To stop the execution of a thread, use the Abort method, as in the following code:

TestThread.Abort()

Besides starting and stopping threads, you can also pause threads by calling the Sleep or Suspend method, resume a suspended thread with the Resume method, and destroy a thread by using the Abort method, as in the following code:

TestThread.Sleep(1000)
TestThread.Abort()

Thread Methods

The following table shows some of the methods that you can use to control individual threads.

Method

Action

Start

Causes a thread to start to run.

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 time-out value, this method returns True if the thread finishes in the allocated time.

Safe Points

Most of these methods are self-explanatory, but the concept of safe points may be new to you. Safe points are locations 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 method of a thread, the common language runtime analyzes the code and determines the location of an appropriate location for the thread to stop running.

Thread Properties

Threads also contain several useful properties, as shown in the following table:

Property

Value

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 a background thread does not prevent a process from stopping. Once all foreground threads that belong to a process have stopped, 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 frequently used to discover individual threads when you debug.

Priority

Gets or sets a value that is 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 about thread states and methods.

Thread Priorities

Every thread has a priority property that determines how big or small a slice of processor time it has to execute. The operating system allocates longer time slices to high-priority threads and shorter time slices to low-priority threads. New threads are created with the value of Normal, but you can change the Priority property to any value in the ThreadPriority enumeration.

See ThreadPriority for a detailed description of the various thread priorities.

Foreground and Background Threads

A foreground thread runs indefinitely, whereas a background thread stops as soon as the last foreground thread has stopped. You can use the IsBackground property to determine or change the background status of a thread.

See Also

Concepts

Thread States

Parameters and Return Values for Multithreaded Procedures

Thread Synchronization

Multithreading with Forms and Controls

Other Resources

Multithreading in Components