Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
54 lines (37 sloc) 4.96 KB
title ms.date ms.technology helpviewer_keywords ms.assetid author ms.author
Using threads and threading
08/08/2018
dotnet-standard
threading [.NET Framework], about threading
managed threading
9b5ec2cd-121b-4d49-b075-222cf26f2344
rpetrusha
ronpet

Using threads and threading

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.

[!NOTE] 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 xref:System.Threading.Tasks.Parallel?displayProperty=nameWithType and xref:System.Threading.Tasks.Task?displayProperty=nameWithType classes, Parallel LINQ (PLINQ), new concurrent collection classes in the xref:System.Collections.Concurrent?displayProperty=nameWithType 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).

How to: Create and start a new thread

You create a new thread by creating a new instance of the xref:System.Threading.Thread?displayProperty=nameWithType 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 xref:System.Threading.Thread.Start%2A?displayProperty=nameWithType method. For more information and examples, see the Creating threads and passing data at start time article and the xref:System.Threading.Thread API reference.

How to: Stop a thread

To terminate the execution of a thread, use the xref:System.Threading.Thread.Abort%2A?displayProperty=nameWithType method. That method raises a xref:System.Threading.ThreadAbortException on the thread on which it's invoked. For more information, see Destroying threads.

Beginning with the .NET Framework 4, you can use the xref:System.Threading.CancellationToken?displayProperty=nameWithType to cancel a thread cooperatively. For more information, see Cancellation in managed threads.

Use the xref:System.Threading.Thread.Join%2A?displayProperty=nameWithType method to make the calling thread wait for the termination of the thread on which the method is invoked.

How to: Pause or interrupt a thread

You use the xref:System.Threading.Thread.Sleep%2A?displayProperty=nameWithType method to pause the current thread for a specified amount of time. You can interrupt a blocked thread by calling the xref:System.Threading.Thread.Interrupt%2A?displayProperty=nameWithType method. For more information, see Pausing and interrupting threads.

Thread properties

The following table presents some of the xref:System.Threading.Thread properties:

Property Description
xref:System.Threading.Thread.IsAlive%2A Returns true if a thread has been started and has not yet terminated normally or aborted.
xref:System.Threading.Thread.IsBackground%2A Gets or sets a Boolean that indicates if a thread is a background thread. Background threads are like foreground threads, but a background thread doesn't 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 xref:System.Threading.Thread.Abort%2A method on background threads that are still alive. For more information, see Foreground and Background Threads.
xref:System.Threading.Thread.Name%2A Gets or sets the name of a thread. Most frequently used to discover individual threads when you debug.
xref:System.Threading.Thread.Priority%2A Gets or sets a xref:System.Threading.ThreadPriority value that is used by the operating system to prioritize thread scheduling. For more information, see Scheduling threads and the xref:System.Threading.ThreadPriority reference.
xref:System.Threading.Thread.ThreadState%2A Gets a xref:System.Threading.ThreadState value containing the current states of a thread.

See also

You can’t perform that action at this time.