You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.0 KiB
C#

/*
* 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;
}
}
/*
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