C#/.NET - stop thread (unsafe way with Thread Abort() method)
In this short article, we want to show you how to stop thread using Thread
Abort()
method in C# / .NET.
Warning: in .NET 5.0
Thread
Abort()
method was marked as deprecated and we shouldn't use that approach to stop threads.
The method presented in the article should be called an unsafe way to stop a thread, because was leading to many mistakes, requiring to have better knowledge of how to use it. Stopping the thread that way, we cannot predict during what operation the thread will be stopped. It may lead to losing consistency on objects shared across multiple threads.
Hint: go to this article to find safe way to stop thread.
Practical example:
xxxxxxxxxx
using System;
using System.Threading;
public class Program
{
public static void Main(string[] args)
{
int[] counters = new int[100];
Thread thread = new Thread(() =>
{
while (true)
IncrementValues(counters);
});
Console.WriteLine("Starting");
thread.Start();
Thread.Sleep(1000); // sleeps main thread for 1 seconds
thread.Abort(); // terminates created thread (it breaks blocking methods when are used too)
Console.WriteLine("Stopped");
PrintValues(counters); // Is there some value in counters array that is different?
// Abort() method breaks logic in the middle of the execution
// loosing objects consistency.
}
// Helper methods
private static void IncrementValues(int[] vaules)
{
for (int i = 0; i < vaules.Length; ++i)
vaules[i] += 1;
}
private static void PrintValues(int[] vaules)
{
for (int i = 0; i < vaules.Length; ++i)
Console.Write(" " + vaules[i]);
}
}
Example output 1:
xxxxxxxxxx
Starting
Stopped
6552799 6552799 6552799 6552799 6552799 6552799 6552799 6552799 6552799 6552799 6552799
6552799 6552799 6552799 6552799 6552799 6552799 6552799 6552799 6552799 6552799 6552799
6552798 6552798 6552798 6552798 6552798 6552798 6552798 6552798 6552798 6552798 6552798
6552798 6552798 6552798 6552798 6552798 6552798 6552798 6552798 6552798 6552798 6552798
6552798 6552798 6552798 6552798 6552798 6552798 6552798 6552798 6552798 6552798 6552798
6552798 6552798 6552798 6552798 6552798 6552798 6552798 6552798 6552798 6552798 6552798
6552798 6552798 6552798 6552798 6552798 6552798 6552798 6552798 6552798 6552798 6552798
6552798 6552798 6552798 6552798 6552798 6552798 6552798 6552798 6552798 6552798 6552798
6552798 6552798 6552798 6552798 6552798 6552798 6552798 6552798 6552798 6552798 6552798
6552798
Example output 2:
xxxxxxxxxx
Starting
Stopped
6724366 6724366 6724366 6724366 6724366 6724366 6724366 6724366 6724366 6724366 6724366
6724366 6724366 6724366 6724366 6724366 6724366 6724366 6724366 6724366 6724366 6724366
6724366 6724366 6724366 6724366 6724366 6724366 6724366 6724366 6724366 6724366 6724366
6724366 6724366 6724366 6724366 6724366 6724366 6724366 6724366 6724366 6724366 6724366
6724366 6724366 6724366 6724366 6724366 6724366 6724366 6724366 6724366 6724366 6724366
6724366 6724366 6724366 6724366 6724366 6724366 6724366 6724366 6724366 6724366 6724366
6724366 6724366 6724366 6724365 6724365 6724365 6724365 6724365 6724365 6724365 6724365
6724365 6724365 6724365 6724365 6724365 6724365 6724365 6724365 6724365 6724365 6724365
6724365 6724365 6724365 6724365 6724365 6724365 6724365 6724365 6724365 6724365 6724365
6724365
Outputs summary:
In the output 1 case, we can see the difference between 4th and 5th lines (6552799
and 6552798
).
In the output 2 case, we can see the difference in the 9th line (6724366
and 6724365
).
Final conclusion:
While different run, the thread was stopped at a different moment in the middle of IncrementValues(counters)
method, which makes it difficult to predict the final object state.