如何启动一个线程停止 [英] How to start a stopped thread

查看:447
本文介绍了如何启动一个线程停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个新的线程,并从主线程启动

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;
    }
}



5秒后,该线程将完成其的ThreadState停止。我想,当用户点击按钮重新启动,但我得到了 ThreadStateException(线程正在运行或终止;它无法重新启动)

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

请帮我如何重新启动已停止线程。谢谢你。

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

推荐答案

如果你想每次重用线程没有新的线程,可以考虑线程池的实现。

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

这篇关于如何启动一个线程停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆