Sunday, 3 December 2017

C# Program to Kill a Thread


Code:

using System;
using System.Threading.Tasks;
using System.Threading;
class Program  
{  
     static void Main(string[] args)  
     {  

         ThreadingClass th = new ThreadingClass();  
         Thread thread1 = new Thread(th.DoStuff);
         thread1.Start();  
         Console.WriteLine("Press any key to exit!!!");  
         Console.ReadKey();  
         th.Stop();
         thread1.Join();  
     }   
}
public class ThreadingClass  
{  
     private bool flag = false;  
    public void DoStuff()  
    {  
         while (!flag)  
         {  
             Console.WriteLine(" Thread is Still Working");  
             Thread.Sleep(1000);  
         }  
     }  
    public void Stop()  
     {  
         flag = true;  
     }  
 }


Output:

Press any key to exit!!!
 Thread is Still Working
 Thread is Still Working
 Thread is Still Working
 Thread is Still Working
 Thread is Still Working
 Thread is Still Working
 Thread is Still Working
 Thread is Still Working
 Thread is Still Working



More C# Programs:


















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...