Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start a stopped thread

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.

like image 561
Leo Vo Avatar asked Jul 05 '11 04:07

Leo Vo


People also ask

How do I start a stopped thread?

Calling the start() function on a terminated thread will result in a RuntimeError indicating that threads can only be started once. Instead, to restart a thread in Python, you must create a new instance of the thread with the same configuration and then call the start() function.

How do you restart a stopped thread in Java?

Once a thread enters dead state it cannot be restarted.

How do I start a thread again?

No. After starting a thread, it can never be started again. If you does so, an IllegalThreadStateException is thrown. In such case, thread will run once but for second time, it will throw exception.

How do you stop and start a thread in Python?

You can't actually stop and then restart a thread since you can't call its start() method again after its run() method has terminated. However you can make one pause and then later resume its execution by using a threading. Condition variable to avoid concurrency problems when checking or changing its running state.


2 Answers

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.

like image 125
Daniel Winks Avatar answered Sep 19 '22 15:09

Daniel Winks


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.

like image 31
Ben Voigt Avatar answered Sep 19 '22 15:09

Ben Voigt