programming-examples/c-sharp/Games_&_Threads/C# Program to Kill a Thread.cs
2019-11-15 12:59:38 +01:00

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