gpt4 book ai didi

C# : How to pause the thread and continue when some event occur?

转载 作者:可可西里 更新时间:2023-11-01 03:06:40 26 4
gpt4 key购买 nike

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

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

最佳答案

你可以使用 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
}

关于C# : How to pause the thread and continue when some event occur?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4848064/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com