暂停/恢复线程蒙山的AutoResetEvent [英] Pause/Resume thread whith AutoResetEvent

查看:139
本文介绍了暂停/恢复线程蒙山的AutoResetEvent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此code我想暂停/继续使用的AutoResetEvent和布尔变量的线程。 是否可以暂停内部消除(用于工作循环中())测试每个时间,如果封锁==真的吗? 测试封杀变量需求也锁定,我认为这是费时的。

  MyClass类
    {
        的AutoResetEvent wait_handle =新的AutoResetEvent();
        布尔受阻= FALSE;

        无效启动()
        {
            线程线程=新主题(工作);
            thread.Start();
        }

        无效暂停()
        {
            封锁= TRUE;
        }

        无效简历()
        {
            封锁= FALSE;
            wait_handle.Set();
        }

        私人无效工作()
        {
            的for(int i = 0; I< 1000000;我++)
            {
                如果(阻止)
                    wait_handle.WaitOne();

                Console.WriteLine(ⅰ);
            }
        }
    }
 

解决方案

是的,你能避免您是通过执行测试一个的ManualResetEvent

的ManualResetEvent 将让你的线程传递,只要它被套(信号),但不同的是的AutoResetEvent 你有previously,它不会自动为线程传递给它重新设置。这意味着你可以将它设置为允许工作在你的循环,并能重置暂停:

  MyClass类
{
    //设置复位事件被初步信号,从而使工作,直到暂停被调用。

    ManualResetEvent的wait_handle =新的ManualResetEvent(真正的);

    无效启动()
    {
        线程线程=新主题(工作);
        thread.Start();
    }

    无效暂停()
    {

        wait_handle.Reset();
    }

    无效简历()
    {
        wait_handle.Set();
    }

    私人无效工作()
    {
        的for(int i = 0; I< 1000000;我++)
        {
            //只要这个等待处理时,此循环将执行。
            //只要它被重置,循环将停止执行并阻止在这里。
            wait_handle.WaitOne();

            Console.WriteLine(ⅰ);
        }
    }
}
 

In this code I want to Pause/Resume a thread using an AutoResetEvent and a bool variable. Is is possible to Pause whithout testing each time (in for loop of Work()) if blocked==true? Testing of "blocked" variable needs locking also and i think this is time consuming.

class MyClass
    {
        AutoResetEvent wait_handle = new AutoResetEvent();
        bool blocked = false;

        void Start()
        {
            Thread thread = new Thread(Work);
            thread.Start();
        }

        void Pause()
        {
            blocked = true;
        }

        void Resume()
        {
            blocked = false;
            wait_handle.Set();
        }

        private void Work()
        {
            for(int i = 0; i < 1000000; i++)
            {
                if(blocked)
                    wait_handle.WaitOne();

                Console.WriteLine(i);
            }
        }
    }

解决方案

Yes, you can avoid the test you are performing by using a ManualResetEvent.

The ManualResetEvent will let your thread pass as long as it is "set" (signalled), but unlike the AutoResetEvent you had previously, it doesn't automatically reset as a thread passes it. This means you can leave it Set to allow work in your loop, and can Reset it to pause:

class MyClass
{  
    // set the reset event to be signalled initially, thus allowing work until pause is called.

    ManualResetEvent wait_handle = new ManualResetEvent (true);

    void Start()
    {
        Thread thread = new Thread(Work);
        thread.Start();
    }

    void Pause()
    {

        wait_handle.Reset();
    }

    void Resume()
    {
        wait_handle.Set();
    }

    private void Work()
    {
        for(int i = 0; i < 1000000; i++)
        {
            // as long as this wait handle is set, this loop will execute.
            // as soon as it is reset, the loop will stop executing and block here.
            wait_handle.WaitOne();

            Console.WriteLine(i);
        }
    }
}

这篇关于暂停/恢复线程蒙山的AutoResetEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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