9

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?

Arion
  • 31,011
  • 10
  • 70
  • 88
Shashwat
  • 2,538
  • 7
  • 37
  • 56
  • 1
    In general, `Abort()`ing a thread is a bad idea. What problem do you want to solve? – Heinzi Mar 06 '12 at 10:23
  • This might help you :- [Restarting a thread in C# 4.0][1] [1]: http://stackoverflow.com/questions/1054889/restarting-a-thread-in-net-using-c – Misam Mar 06 '12 at 10:25
  • The requirement to stop and start the thread is a bit unusual - what is the problem you are trying to resolve? – dice Mar 06 '12 at 10:35
  • i have a server thread which first initializes a socket and then repeatedly accepts incoming connections... we can close the server thus closing the socket it is using and aborting the thread... now if i want to start the server again, i need the thread function to run again... – Shashwat Mar 06 '12 at 12:51

5 Answers5

9

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.

Julien
  • 3,509
  • 20
  • 35
  • 2
    Good advice. `run` should be `volatile` to avoid indefinite waiting due to compiler optimizations. – Tudor Mar 06 '12 at 10:34
  • 1
    Consider using `lock`, esspecially when setting the boolean value. Even better, using it in conjuction with `monitor`. Have a look here: http://stackoverflow.com/questions/1559293/c-sharp-monitor-wait-pulse-pulseall#1559662 – YS. Mar 06 '12 at 10:44
  • My thread function starts when I click to create server. It performs some initialization and then goes into an infinite loop containing socket.Accept(). Control gets stuck at this statement so I can't use this lock method. – Shashwat Mar 06 '12 at 12:57
  • 2
    @YS.: Reading or writing a Boolean in .NET is **guaranteed to be atomic**. Thus, no lock or monitor required. http://stackoverflow.com/questions/59422/is-a-bool-read-write-atomic-in-c-sharp – Heinzi Mar 12 '12 at 15:50
  • lock == monitor. See [here](http://msdn.microsoft.com/en-us/library/sy1y3y1t.aspx) or [here](http://stackoverflow.com/questions/4978850/monitor-vs-lock) – Matthias Jul 03 '12 at 21:29
  • But I have a socket waiting for connection acceptance inside it. Even if I set the `run=false`, it will still be stuck at that line. – Shashwat Oct 30 '12 at 08:15
7

create new instance of thread and execute again. thread1= new Thread(); thread1.start();

Siva
  • 130
  • 1
  • 10
2

Thread.Abort does not guarantee that the thread will terminate. For instance, if you have a long running query, Abort will not terminate the execution of the query, or the cancellation of the thread. In fact, the thread will live on until the query completes.

If you're doing everything in managed code and not getting locked up by unmanaged resources, and you must abort a thread, thread.Abort() is perfectly fine.

However, you cannot call Start on a thread that has been terminated. You'll have to create another Thread and call Start on that thread. Thread creation is somewhat expensive, memory wise, in .NET (in comparison with other langauges), so there are some drawbacks.

Candide
  • 30,469
  • 8
  • 53
  • 60
  • Actually creating the thread again and again wont be too expensive as it wont be done very frequently. – Shashwat Mar 06 '12 at 12:58
1

When you want to re-start the thread from the beginning, you actually want to restart an execution of certain function (code flow) on the thread. When you create a thread and pass a function for execution, a thread's life will be terminated as soon as the function finishes its own execution. You just need to change your code design that will allow to restart the function with recreating a new thread. But for short functions I would advise to use ThreadPool.

Slava Kovalchuk
  • 523
  • 4
  • 7
1

Since you are using .NET 4.0, where MS had introduced the "Cooperative Cancellation Framework". You can read more from this blog. Dealing directly with Thread is (more and more) discouraged.

Jiaji Wu
  • 459
  • 3
  • 10