zoukankan      html  css  js  c++  java
  • AutoResetEvent和ManualResetEvent用法示例

    using System;
    using System.Threading;

    namespace ResetEvent{

    public class EntryPoint{
    static AutoResetEvent auto = new AutoResetEvent(false);
    static ManualResetEvent manual = new ManualResetEvent(false);
    static EventWaitHandle eventWait = new EventWaitHandle(false,EventResetMode.AutoReset);

    public static void Main(string[] args){
    eventWait = auto;
    ManualResetEventMethodRun();
    // AutoResetEventMethodRun();
    }
    #region AutoResetEventMethod

    static void AutoResetEventMethodRun(){
    Thread t = new Thread(AutoResetEventMethod);
    Console.WriteLine("AutoResetEvent Main Begin...");
    t.Start();
    Thread.Sleep(5000);
    auto.Set();
    // Thread.Sleep(5000);
    Console.WriteLine("AutoResetEvent Main End...");
    Console.Read();
    }
    static void AutoResetEventMethod(){
    Console.WriteLine("AutoResetEvent Sub Begining......");
    auto.WaitOne();
    // auto.Set();
    Console.WriteLine("AutoResetEvent to do something 1......");
    auto.WaitOne();
    Console.WriteLine("AutoResetEvent to do something 2......");
    Console.WriteLine("AutoResetEvent Sub Ending......");

    }

    #endregion

    #region ManualResetEventMethod

    static void ManualResetEventMethodRun(){
    Thread t = new Thread(ManualResetEventMethod);
    Console.WriteLine("ManualResetEventMethod Main Begin...");
    t.Start();
    Thread.Sleep(5000);
    manual.Set();
    // Thread.Sleep(5000);
    Console.WriteLine("ManualResetEventMethod Main End...");
    Console.Read();
    }

    static void ManualResetEventMethod()
    {
    Console.WriteLine("ManualResetEventMethod Sub Begining......");
    manual.WaitOne();
    Console.WriteLine("ManualResetEventMethod to do something 1......");
    manual.Reset();
    manual.WaitOne();
    Console.WriteLine("ManualResetEventMethod to do something 2......");
    Console.WriteLine("ManualResetEventMethod Sub Ending......");
    }
    #endregion
    }

    }

  • 相关阅读:
    Swift 协议
    Objective C 链式调用
    objective-c 关键字和概念
    如何在Objective-C中实现链式语法?
    _视图控制对象生命周期-init、viewDidLoad、viewWillAppear、viewDidAppear、viewWillDisappear等的区别及用途
    Swift静态方法
    Swift静态属性
    objective-c 中代码块(blocks)
    OS笔记047代理传值和block传值
    Objective-C官方文档翻译 Block
  • 原文地址:https://www.cnblogs.com/zhangchenliang/p/2653268.html
Copyright © 2011-2022 走看看