如何暂停/暂停,然后一个线程继续吗? [英] How to pause/suspend a thread then continue it?

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

问题描述

我正在在C#,它使用一个winform作为GUI和其在自动改变事物后台运行一个单独的线程的应用程序。例如:

 公共无效的run()
{
,而(真)
{
printMessageOnGui(嗨);
Thread.sleep代码(2000);
//做更多的工作
}
}



如何会我能在任何地方暂停在循环,因为循环的一个迭代大约需要30秒。所以,我不希望它完成一个循环后暂停,我想暂停时间。


解决方案

  ManualResetEvent的MRSE =新的ManualResetEvent(假); 

公共无效的run()
{
,而(真)
{
mrse.WaitOne();
printMessageOnGui(嗨);
Thread.sleep代码(2000); 。 。
}
}

公共无效简历()
{
mrse.Set();
}

公共无效暂停()
{
mrse.Reset();
}


I am making an application in C# which uses a winform as the GUI and a separate thread which is running in the background automatically changing things. Ex:

public void Run()
{
    while(true)
    {
        printMessageOnGui("Hey");
        Thread.Sleep(2000);
        // Do more work
    } 
}

How would I make it pause anywhere in the loop, because one iteration of the loop takes around 30 seconds. So I wouldn't want to pause it after its done one loop, I want to pause it on time.

解决方案

ManualResetEvent mrse = new ManualResetEvent(false);

public void run() 
{ 
    while(true) 
    { 
        mrse.WaitOne();
        printMessageOnGui("Hey"); 
        Thread.Sleep(2000); . . 
    } 
}

public void Resume()
{
    mrse.Set();
}

public void Pause()
{
    mrse.Reset();
}

这篇关于如何暂停/暂停,然后一个线程继续吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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