Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restart a Thread in C# .Net 4.0?

I'm using C# .Net4.0 in VS 2010. How do I restart a Thread?

Its like I want to Abort() the thread and Start() it from the beginning again? Is it possible?

like image 792
Shashwat Avatar asked Mar 06 '12 10:03

Shashwat


1 Answers

Abort a thread is often a bad idea. He is an advisor. If it's an infinite loop, a boolean used to stop the thread without the abortion.

bool run = true;
Thread thread = new Thread(method);
thread.start();

private void method()
{
  while(run)
  {

  }
}

To stop the thread, just set the boolean to false and normally, you can restart it later.

like image 161
Julien Avatar answered Oct 02 '22 08:10

Julien