14

I create a new thread and start it from a main thread.

m_MyThread = new Thread(HandleMyThread);
m_MyThread.IsBackground = true;
m_MyThread.Start();

private void HandleMyThread()
{
    while (true)
    {
        Thread.Sleep(5000);
        return;
    }
}

After 5 seconds, this thread will finish and its ThreadState is Stopped. I want to start it again when user clicks on button but I get a ThreadStateException (Thread is running or terminated; it cannot restart):

private void button1_Click(object sender, EventArgs e)
{
    m_MyThread.Start(); // ->raise exception
}

Please help me how to restart a stopped thread. Thanks.

luviktor
  • 2,240
  • 2
  • 22
  • 23
Leo Vo
  • 9,980
  • 9
  • 56
  • 78
  • 1
    For additional information to stop a thread look to my question: [Proper way to stop a threaded job](http://stackoverflow.com/questions/5146186/proper-way-to-stop-a-threaded-job-forced) – Gerard Jul 11 '11 at 18:08
  • @Leovo take a look in this : https://learn.microsoft.com/en-us/dotnet/standard/threading/pausing-and-resuming-threads#interrupting-threads isn't an answer, but can helps. – antonio Oct 25 '19 at 04:27

4 Answers4

16

I know this question is a bit old, but I thought I would post a response in case others come here.

For this example code, if it were changed to look like this:

Thread m_MyThread;
private void HandleMyThread()
{
    while (true)
    {
        Thread.Sleep(5000);
        return;
    }
}

private void button1_Click(object sender, EventArgs e)
{
    if (!m_MyThread.IsAlive)
    {
        m_MyThread = new Thread(HandleMyThread);
        m_MyThread.IsBackground = true;
        m_MyThread.Start();
    }
}

This would create a new instance of the thread and start it. The ThreadStateException error is because, simply, you can't re-start a thread that's in a stopped state. m_MyThread.Start() is only valid for threads in the Unstarted state. What needs done in cases like this is to create a new thread instance and invoke Start() on the new instance.

Daniel Winks
  • 345
  • 3
  • 9
3

Use a ManualResetEvent, and instead of Thread.Sleep, wait for the event with a timeout.

Then, any other thread can activate the event, and immediately resume the sleeping thread.

Once a thread is exited, it can no longer run. So don't let it exit. Instead, put it back to sleep waiting for an event.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • I have a problem in my application. When I run my application, I need to create a thread which sleeps to next 8h AM to raise a event. Please give me the way to solve. At moment, I create a thread and calculate total miliseconds from now to 8h AM so that it will sleep in duration of this time. After finish sleeping, It will raise a event. Do you have any ideals about my way? Thanks. – Leo Vo Jul 05 '11 at 06:46
2

Just make a new thread like you did when you initially created the thread. You might also want to pull that out into a method to avoid repeating yourself.

icktoofay
  • 126,289
  • 21
  • 250
  • 231
2

If you want to reuse the thread without new a thread every time , you can consider the implementation of thread pool.

Ivan
  • 766
  • 6
  • 17
  • But in 2 ways, what is the best way because I want to reuse the thread. Please tell me advantages and disadvantages in 2 ways. – Leo Vo Jul 05 '11 at 04:47