C# Program to Kill a Thread

This is a C# Program to kill a thread.

Problem Description

This C# Program Kills a Thread.

Problem Solution

Here the working of the thread is stopped when a key is pressed else the work continues.

Program/Source Code

Here is source code of the C# Program to Kill a Thread. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Kill a Thread
 */
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;  
     }  
 }
Program Explanation

In this C# Program, we are creating an object ‘th’ for ThreadingClass. then we are creating a new thread named as thread1 by calling the DoStuff() function in threading class. Using while loop in Dostuff() function check the thread is still working by checking the condition of the value of ‘flag’ variable is equal to true.

advertisement
advertisement

If the condition is true, then execute the statement and print the statement as Thread is Still Working. The sleep() method is used for making a thread pause for a specific period of time. Here the working of the thread is stopped when a key is pressed else the work continues.

Runtime Test Cases
 
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

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.