zoukankan      html  css  js  c++  java
  • C# AutoResetEvent 理解

    、、

    AutoResetEvent内存中保持着一个bool
    False,则使线程阻塞;值为True,使线程退出阻塞

    创建AutoResetEvent对象的实例,在函数构造中传递默认的bool值;
    AutoResetEvent autoResetEvent = new AutoResetEvent(false);

    autoResetEvent.Reset();  //修改bool值为False;

    autoResetEvent.Set(); //修改bool值为True;
    --------------------------------------------------------------------

    WaitOne() 方法,阻止 线程 继续执行进入等待状态;收到信号返回true,否则返回false;
    WaitOne(毫秒),等待指定秒数,如果指定秒数收到任何信号,那么后续代码继续执行

         AutoResetEvent autoResetEvent = new AutoResetEvent(false);//只对一个线程设置为有信号。
            public Form1()
            {
                InitializeComponent();
                CheckForIllegalCrossThreadCalls = false;
            }
            private void Form1_Load(object sender, EventArgs e){ }
            private void buttonSet_Click(object sender, EventArgs e)
            {
                autoResetEvent.Set();//设置为True 时 WriteMsg() 会跳出循环,停止输出:Continue;
            }
            private void buttonStart_Click(object sender, EventArgs e)
            {
                Thread td = new Thread(WriteMsg);
                td.Start();
            }
            /// <summary>
            /// 输出消息
            /// </summary>
            public void WriteMsg()
            { 
                autoResetEvent.Set();
                autoResetEvent.WaitOne();
                //遇到此行代码,如果autoResetEvent为true,会直接往下执行代码,autoResetEvent为false,会停止在此行代码;
                //因为此行代码之前,执行了Set()方法,所以WaitOne()语句直接执行过。
                textBox1.AppendText("
    vvvvvvv");
                //此时,autoResetEvent 变成 false ; 
                while (!autoResetEvent.WaitOne(TimeSpan.FromSeconds(2)))//原值为:false,加上!号变为:true ;
                {
                    textBox1.AppendText("
    Continue");
                    Thread.Sleep(TimeSpan.FromSeconds(1));
                } 
                bool bl10 = autoResetEvent.WaitOne();
                //跳出循环后,执行到此行代码时,因为autoResetEvent又变为false,所以就停止在了此处,没有继续执行。
                //再次执行autoResetEvent.Set();方法后就会直接执行完成方法。
            }
            private void buttonReset_Click(object sender, EventArgs e)
            {
                autoResetEvent.Reset();
            }

     ---------------------------------------------------------------------

     /// <summary>
            /// 输出消息
            /// </summary>
            public void WriteMsg()
            {
                autoResetEvent.WaitOne(TimeSpan.FromSeconds(5));
                //如果没有Reset() 和 Set()方法被调用,当执行到此行代码时,会等待5秒钟,才会继续执行后面的代码;
                //如果执行了.Set()方法,就会立马继续执行后面代码,不会继续等待5秒之后。
                textBox1.AppendText("
    Continue");
                Thread.Sleep(TimeSpan.FromSeconds(1));
            }

        

    ‘’

  • 相关阅读:
    怎样通过iPhone Safari 来安装测试版ipa
    UINavigationController检测后退按钮被按下
    Android系统在超级终端下必会的命令大全(adb shell命令大全)
    so 加载符号找不到的问题解决
    Ubuntu Android 开发jdk1.5和1.6之间的切换
    UIView animation
    HTC G11 获取Root权限图文教程
    Android.mk文件语法规范
    Android NDK 下的宽字符编码转换及icu库的使用
    [转载].NET制作安装卸载程序 Virus
  • 原文地址:https://www.cnblogs.com/lanyubaicl/p/11170698.html
Copyright © 2011-2022 走看看