c# terminate thread safely code example

Example: c# kill thread

/* You can use the Abort() method to stop a thread.

NOTE: If the thread that calls Abort() holds a lock, 
and the aborted thread requires that lock to be released, 
then a deadlock can occur. */

using System;
using System.Threading;

class ThreadExample 
{
	public void MyThread()
	{
        for (int x = 0; x < 1000; x++) 
        {
            Console.WriteLine(x);
        }
	}
}

class MainClass 
{
	public static void Main()
    {
		var obj = new ThreadExample();
		Thread thr = new Thread(new ThreadStart(obj.MyThread));
		thr.Start();
		
        // Abort!
        thr.Abort();
	}
}