C#:当某些事件发生时如何暂停线程并继续?

发布于 2024-10-15 04:58:11 字数 93 浏览 2 评论 0原文

当某些事件发生时,如何暂停线程并继续?

我希望单击按钮时线程继续。 有人告诉我 thread.suspend 不是暂停线程的正确方法。 还有其他解决方案吗?

How can I pause a thread and continue when some event occur?

I want the thread to continue when a button is clicked.
Someone told me that thread.suspend is not the proper way to pause a thread.
Is there another solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

行雁书 2024-10-22 04:58:11

您可以使用 System.Threading.EventWaitHandle

EventWaitHandle 会阻塞,直到收到信号为止。在您的情况下,它将由按钮单击事件发出信号。

private void MyThread()
{
    // do some stuff

    myWaitHandle.WaitOne(); // this will block until your button is clicked

    // continue thread
}

您可以像这样发出等待句柄信号:

private void Button_Click(object sender, EventArgs e)
{
     myWaitHandle.Set(); // this signals the wait handle and your other thread will continue
}

You could use a System.Threading.EventWaitHandle.

An EventWaitHandle blocks until it is signaled. In your case it will be signaled by the button click event.

private void MyThread()
{
    // do some stuff

    myWaitHandle.WaitOne(); // this will block until your button is clicked

    // continue thread
}

You can signal your wait handle like this:

private void Button_Click(object sender, EventArgs e)
{
     myWaitHandle.Set(); // this signals the wait handle and your other thread will continue
}
缪败 2024-10-22 04:58:11

事实上,挂起线程是一种不好的做法,因为您很少确切知道线程当时正在做什么。让线程运行经过 ManualResetEvent 更容易预测,每次调用WaitOne()。这将充当门 - 控制线程可以调用 Reset() 来关闭门(暂停线程,但安全),并调用 Set() 打开门(恢复线程)。

例如,您可以在每次循环迭代开始时调用 WaitOne(如果循环太紧,则每 n 迭代调用一次)。

Indeed, suspending a thread is bad practice since you very rarely know exactly what a thread is doing at the time. It is more predictable to have the thread run past a ManualResetEvent, calling WaitOne() each time. This will act as a gate - the controlling thread can call Reset() to shut the gate (pausing the thread, but safely), and Set() to open the gate (resuming the thread).

For example, you could call WaitOne at the start of each loop iteration (or once every n iterations if the loop is too tight).

层林尽染 2024-10-22 04:58:11

你也可以尝试这个

private static AutoResetEvent _wait = new AutoResetEvent(false);

public Form1()
    {
        InitializeComponent();
    }

private void Form1_Load(object sender, EventArgs e)
    {
        Control.CheckForIllegalCrossThreadCalls = false;
        backgroundWorker1.RunWorkerAsync();
    }
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        Dosomething();
    }

private void Dosomething()
{
 //Your Loop
 for(int i =0;i<10;i++)
   {
    //Dosomething
    _wait._wait.WaitOne();//Pause the loop until the button was clicked.

   } 
}

private void btn1_Click(object sender, EventArgs e)
    {
        _wait.Set();
    }

You can try this also

private static AutoResetEvent _wait = new AutoResetEvent(false);

public Form1()
    {
        InitializeComponent();
    }

private void Form1_Load(object sender, EventArgs e)
    {
        Control.CheckForIllegalCrossThreadCalls = false;
        backgroundWorker1.RunWorkerAsync();
    }
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        Dosomething();
    }

private void Dosomething()
{
 //Your Loop
 for(int i =0;i<10;i++)
   {
    //Dosomething
    _wait._wait.WaitOne();//Pause the loop until the button was clicked.

   } 
}

private void btn1_Click(object sender, EventArgs e)
    {
        _wait.Set();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文