Good point @nmaillet. Use boolean (volatile) instance variable to control a thread.
public class Best
{
volatile bool flag=true ;
Thread thread;
public Best()
{
thread=new Thread(new ThreadStart(Run));
thread.Start();
}
public void Stop()
{
flag = false;
}
public void Run()
{
while (flag)
{
Console.WriteLine("{0}", DateTime.Now.ToShortTimeString());
}
}
}
Best a = new Best();
Thread.Sleep(1000);
a.Stop(); // Stop this thread after approximate 1000 millsec.
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
Offline 6,527 posts
since Oct 2008