C# Thread Abort
To terminate the thread, the Abort() method is used in C#. In case the Abort operation is not done, the ThreadAbortException exception is raised.
Example:
using System; using System.Threading; public class Example { public void thrd1() { for (char i = 'a'; i < 'g'; i++) { Console.WriteLine(i); Thread.Sleep(100); } } } public class thrd2 { public static void Main() { Console.WriteLine("Hello World!!"); Example thrd = new Example(); Thread x = new Thread(new ThreadStart(thrd.thrd1)); Thread y = new Thread(new ThreadStart(thrd.thrd1)); x.Start(); y.Start(); try { x.Abort(); y.Abort(); } catch (ThreadAbortException except) { Console.WriteLine(except.ToString()); } Console.WriteLine("Bye Bye!!"); } } |
Output:
Explanation:
In the above example, we are displaying the use and behavior of the Abort method in C#. Here, the thread may be in running state, thus the output is unpredictable.