AutoResetEvent 笔记2

 WaitOne方法将当前线程置于Sleep状态。如果WaitOne方法收到信号则返回true否则返回false

程序5秒 刷新一次 页面数据,每次刷新数据 动加1

 AutoResetEvent autoResetEvent = new AutoResetEvent(false);
        private void button1_Click(object sender, EventArgs e)
        {
            int i = 0;
            while (!autoResetEvent.WaitOne(TimeSpan.FromSeconds(5)))
            {
                this.textBox1.Text = i.ToString();
                i = i + 1;
            }
        }

 

 

new AutoResetEvent(false),下面的循环,只要执行到auto.WaitOne()就会暂停下来,只有auto.Set()时才会继续执行下去;
new AutoResetEvent(true),下面的循环,第一次到代码auto.WaitOne()时,不会停下来,第二次才会停下来;

        AutoResetEvent auto = new AutoResetEvent(false);
        private void button1_Click(object sender, EventArgs e)
        {
            Thread t = new Thread(() =>
            {
                int i = 0;
                while (true)
                {
                    bool bl = auto.WaitOne();
                }
            });
            t.Start();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            auto.Set();
        }

 

 

 

 

 

 

 

 

 

posted @ 2020-09-12 10:59  蓝雨冰城  阅读(176)  评论(0编辑  收藏  举报