VOOZH about

URL: https://www.geeksforgeeks.org/c-sharp/how-to-terminate-a-thread-in-c-sharp/

⇱ How to Terminate a Thread in C#? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Terminate a Thread in C#?

Last Updated : 20 Apr, 2026

In C#, threads are used to achieve tasks concurrently, a fundamental feature in multithreading and parallel programming. However, there are scenarios where we need to terminate a thread. There are different ways to terminate a thread and in this article, we will discuss those ways with their code implementation.

Ways to Terminate Thread

  • Abort Method
  • Abort Object
  • CancellationToken
  • Manually

1. Using Abort Method

The Abort() method throws a ThreadAbortException to the thread in which it is called, forcing the thread to terminate. However, this method is deprecated due to unpredictable behavior and is no longer recommended for use in modern versions of .NET.

Syntax:

public void Abort();

Exceptions:

  • SecurityException: If the caller does not have the required permission.
  • ThreadStateException: If the thread that is being aborted is currently suspended.

Example:

Output:

👁 SecruityExceptionAndWarningAbortMeth

Explanation: The above example shows the use of the Abort() method which is provided by the Thread class. By using thr.Abort(); statement, we can terminate the execution of the thread.

Note: This method is deprecated and is no longer used in newer versions of .NET. Instead of using Abort(), prefer safer alternatives like CancellationToken.

2. Using Abort Object

This method raises a ThreadAbortException in the thread on which it is invoked, to begin the process of terminating the thread while also providing exception information about the thread termination. Same as Abort method this is also not recommended to use.

Syntax:

public void Abort(object information);

Here, the information contains any information that you want to pass in a thread when it is being stopped. This information is only accessible by using the ExceptionState property of ThreadAbortException

Exceptions:

  • SecurityException: If the caller does not have the required permission.
  • ThreadStateException: If the thread that is being aborted is currently suspended.

Example:

Output:

👁 AbortObjectExceptionWithWarning

Note:Abort(Object) is also not in used in newer versions and if we try to to execute the above program it will give the warning as shown in the output picture.

3. Using CancellationToken

CancellationToken allows us to politely request that a thread should stop its work. It is recommended to use in newer versions. We need to create a CancellationTokenSource object and pass its token to the thread.

Syntax

CancellationTokenSource tokenSource = new CancellationTokenSource(); // create object

Thread workerThread = new Thread(() => LongRunningOperation(tokenSource.Token));

if (token.IsCancellationRequested)

{

// Perform cleanup and exit

return;

}

tokenSource.Cancel();

Example:


Output
Working... Step 1
Working... Step 2
Working... Step 3
Work completed.
Main thread exits.

4. Manual Termination

We can also terminate a thread manually just like creating a flag that the thread checks regularly to see if it should stop. This is a manual and simple method, but it requires the thread to periodically check the flag and decide when to exit.

Syntax

private volatile bool stopRequested; // Flag to signal thread termination

public void DoWork()

{

while (!stopRequested) // Check flag periodically

{
// Thread work here

Console.WriteLine("Working...");

Thread.Sleep(100); // Simulate work
}

}

public void RequestStop() => stopRequested = true;

Example:


Output
Working... Step 1
Working... Step 2
Stop requested, terminating the thread.
Main thread exits.

Key Points about Abort()

  • Calling Abort() can cause a deadlock if the thread holds a lock required by another thread.
  • If Abort() is called before Start(), the thread will abort when it starts; if called during sleep or blocking, the thread is interrupted and then aborted; if called on a suspended thread, it throws a ThreadStateException.
  • ThreadAbortException is thrown only when the thread returns to managed code and is delayed if the thread is suspended or executing unmanaged code.
  • Multiple calls to Abort() may behave unpredictably, and this situation cannot be detected by the application.
  • After calling Abort(), the thread state becomes AbortRequested, and after termination, it changes to Stopped, but it can be cancelled using ResetAbort() if allowed.
Comment

Explore