NOTENOTE
How to: Create and start a new thread
How to: Stop a thread
How to: Pause or interrupt a thread
With .NET, you can write applications that perform multiple operations at the same time. Operations with the
potential of holding up other operations 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.
If you need more control over the behavior of the application's threads, you can manage the threads yourself. However,
starting with the .NET Framework 4, multithreaded programming is greatly simplified with the
System.Threading.Tasks.Parallel and System.Threading.Tasks.Task classes, Parallel LINQ (PLINQ), new concurrent collection
classes in the System.Collections.Concurrent namespace, and a new programming model that is based on the concept of
tasks rather than threads. For more information, see Parallel Programming and Task Parallel Library (TPL).
You create a new thread by creating a new instance of the System.Threading.Thread class and providing the name
of the method that you want to execute on a new thread to the constructor. To start a created thread, call the
Thread.Start method. For more information and examples, see the Creating threads and passing data at start time
article and the Thread API reference.
To terminate the execution of a thread, use the System.Threading.CancellationToken. It provides a unified way to
stop threads cooperatively. For more information, see Cancellation in managed threads.
Sometimes it is not possible to stop a thread cooperatively, because it runs third-party code not designed for
cooperative cancellation. In this case, you might want to terminate its execution forcibly. To terminate the
execution of a thread forcibly, in .NET Framework you can use the Thread.Abort method. That method raises a
ThreadAbortException on the thread on which it's invoked. For more information, see Destroying threads. The
Thread.Abort method is not supported in .NET Core. If you need to terminate the execution of third-party code
forcibly in .NET Core, run it in the separate process and use Process.Kill.
The System.Threading.CancellationToken is not available before .NET Framework 4. To stop a thread in older .NET
Framework versions, you should implement the cooperative cancellation manually using the thread
synchronization techniques. For example, you can create the volatile boolean field shouldStop and use it to
request the code executed by the thread to stop. For more information, see volatile in C# Reference and
System.Threading.Volatile.
Use the Thread.Join method to make the calling thread wait for the termination of the thread being stopped.
You use the Thread.Sleep method to pause the current thread for a specified amount of time. You can interrupt a
blocked thread by calling the Thread.Interrupt method. For more information, see Pausing and interrupting
threads.