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?

4
  • 1
    In general, Abort()ing a thread is a bad idea. What problem do you want to solve? Commented Mar 6, 2012 at 10:23
  • This might help you :- [Restarting a thread in C# 4.0][1] [1]: stackoverflow.com/questions/1054889/… Commented Mar 6, 2012 at 10:25
  • The requirement to stop and start the thread is a bit unusual - what is the problem you are trying to resolve? Commented Mar 6, 2012 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... Commented Mar 6, 2012 at 12:51

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Good advice. run should be volatile to avoid indefinite waiting due to compiler optimizations.
Consider using lock, esspecially when setting the boolean value. Even better, using it in conjuction with monitor. Have a look here: stackoverflow.com/questions/1559293/…
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.
@YS.: Reading or writing a Boolean in .NET is guaranteed to be atomic. Thus, no lock or monitor required. stackoverflow.com/questions/59422/…
lock == monitor. See here or here
|
7

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

Comments

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.

1 Comment

Actually creating the thread again and again wont be too expensive as it wont be done very frequently.
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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.