简体   繁体   English

如何启动已停止的线程

[英]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. 5 秒后,该线程将完成,其 ThreadState 已停止。 I want to start it again when user clicks on button but I get a ThreadStateException (Thread is running or terminated; it cannot restart) :当用户单击按钮时,我想再次启动它,但我得到一个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.谢谢。

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. ThreadStateException错误是因为简单地说,您无法重新启动已停止的 state 中的线程。 m_MyThread.Start() is only valid for threads in the Unstarted state. m_MyThread.Start()仅对Unstarted state 中的线程有效。 What needs done in cases like this is to create a new thread instance and invoke Start() on the new instance.在这种情况下需要做的是创建一个新的线程实例并在新实例上调用Start()

Use a ManualResetEvent , and instead of Thread.Sleep , wait for the event with a timeout.使用ManualResetEvent ,而不是Thread.Sleep ,等待具有超时的事件。

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.相反,让它重新进入睡眠状态等待事件。

If you want to reuse the thread without new a thread every time, you can consider the implementation of thread pool.如果你想重用线程而不每次都新建一个线程,可以考虑线程池的实现。

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.您可能还想将其提取到一种方法中以避免重复自己。

To restart a thread, try this:要重新启动线程,请尝试以下操作:

private void button1_Click(object sender, EventArgs e)
{
    // Just create a new constructor for the stopped Thread
    m_MyThread = null;
    m_MyThread = new Thread(HandleMyThread);
    m_MyThread.IsBackground = true;
    // Then restart the stopped Thread
    m_MyThread.Start();
}

This works if the thread was previously stopped.如果线程先前已停止,则此方法有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM