C#:如何暂停线程,当一些事件继续发生? [英] C# : How to pause the thread and continue when some event occur?

查看:401
本文介绍了C#:如何暂停线程,当一些事件继续发生?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何暂停线程,当一些事件发生的继续?

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

我想线程继续单击按钮时。
有人告诉thread.suspend不是暂停线程的正确方法。
接着另外一个解决方案?

I want the thread continue when the button click. Someone tell that thread.suspend is not the proper way to pause the thread. Then another solution?

推荐答案

您可以使用的 System.Threading.EventWaitHandle

这是块的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
}


$直到b $ b

您可以通知你的等待句柄是这样的:

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
}

这篇关于C#:如何暂停线程,当一些事件继续发生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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