zoukankan      html  css  js  c++  java
  • ManualResetEvent 类

    http://msdn.microsoft.com/zh-cn/library/system.threading.manualresetevent(v=vs.80).aspx

    通知一个或多个正在等待的线程已发生事件。无法继承此类。

    命名空间:System.Threading
    程序集:mscorlib(在 mscorlib.dll 中)

    [ComVisibleAttribute(true)] 
    public sealed class ManualResetEvent : EventWaitHandle
    

      

    备注:

    Note注意

    应用于此类的 HostProtectionAttribute 属性 (Attribute) 具有以下 Resources 属性 (Property) 值:Synchronization | ExternalThreadingHostProtectionAttribute 并不会影响桌面应用程序(桌面应用程序通常通过双击图标、键入命令或在浏览器中输入 URL 来启动)。有关更多信息,请参见 HostProtectionAttribute 类或 SQL Server 编程和宿主保护属性

    在 .NET Framework 2.0 版中,ManualResetEvent 从新的 EventWaitHandle 类派生。ManualResetEvent 在功能上等效于用 EventResetMode.ManualReset 创建的EventWaitHandle

    Note注意

    与 ManualResetEvent 类不同,EventWaitHandle 类提供对已命名的系统同步事件的访问。

    ManualResetEvent 允许线程通过发信号互相通信。通常,此通信涉及一个线程在其他线程进行之前必须完成的任务。

    当一个线程开始一个活动(此活动必须完成后,其他线程才能开始)时,它调用 Reset 以将 ManualResetEvent 置于非终止状态。此线程可被视为控制 ManualResetEvent。调用ManualResetEvent 上的 WaitOne 的线程将阻止,并等待信号。当控制线程完成活动时,它调用 Set 以发出等待线程可以继续进行的信号。并释放所有等待线程。

    一旦它被终止,ManualResetEvent 将保持终止状态,直到它被手动重置。即对 WaitOne 的调用将立即返回。

    可以通过将布尔值传递给构造函数来控制 ManualResetEvent 的初始状态,如果初始状态处于终止状态,为 true;否则为 false

    ManualResetEvent 也可以同 staticWaitAll 和 WaitAny 方法一起使用。

    有关线程同步机制的更多信息,请参见概念文档中的 ManualResetEvent

    示例

    下面的代码示例阐释了如何使用等待句柄来发送复杂数字计算的不同阶段的完成信号。此计算的格式为:result = first term + second term + third term,其中每项都要求使用计算出的基数进行预计算和最终计算。

    using System;
    using System.Threading;
    
    class CalculateTest
    {
        static void Main()
        {
            Calculate calc = new Calculate();
            Console.WriteLine("Result = {0}.", 
                calc.Result(234).ToString());
            Console.WriteLine("Result = {0}.", 
                calc.Result(55).ToString());
        }
    }
    
    class Calculate
    {
        double baseNumber, firstTerm, secondTerm, thirdTerm;
        AutoResetEvent[] autoEvents;
        ManualResetEvent manualEvent;
    
        // Generate random numbers to simulate the actual calculations.
        Random randomGenerator;
    
        public Calculate()
        {
            autoEvents = new AutoResetEvent[]
            {
                new AutoResetEvent(false),
                new AutoResetEvent(false),
                new AutoResetEvent(false)
            };
    
            manualEvent = new ManualResetEvent(false);
        }
    
        void CalculateBase(object stateInfo)
        {
            baseNumber = randomGenerator.NextDouble();
    
            // Signal that baseNumber is ready.
            manualEvent.Set();
        }
    
        // The following CalculateX methods all perform the same
        // series of steps as commented in CalculateFirstTerm.
    
        void CalculateFirstTerm(object stateInfo)
        {
            // Perform a precalculation.
            double preCalc = randomGenerator.NextDouble();
    
            // Wait for baseNumber to be calculated.
            manualEvent.WaitOne();
    
            // Calculate the first term from preCalc and baseNumber.
            firstTerm = preCalc * baseNumber * 
                randomGenerator.NextDouble();
    
            // Signal that the calculation is finished.
            autoEvents[0].Set();
        }
    
        void CalculateSecondTerm(object stateInfo)
        {
            double preCalc = randomGenerator.NextDouble();
            manualEvent.WaitOne();
            secondTerm = preCalc * baseNumber * 
                randomGenerator.NextDouble();
            autoEvents[1].Set();
        }
    
        void CalculateThirdTerm(object stateInfo)
        {
            double preCalc = randomGenerator.NextDouble();
            manualEvent.WaitOne();
            thirdTerm = preCalc * baseNumber * 
                randomGenerator.NextDouble();
            autoEvents[2].Set();
        }
    
        public double Result(int seed)
        {
            randomGenerator = new Random(seed);
    
            // Simultaneously calculate the terms.
            ThreadPool.QueueUserWorkItem(
                new WaitCallback(CalculateBase));
            ThreadPool.QueueUserWorkItem(
                new WaitCallback(CalculateFirstTerm));
            ThreadPool.QueueUserWorkItem(
                new WaitCallback(CalculateSecondTerm));
            ThreadPool.QueueUserWorkItem(
                new WaitCallback(CalculateThirdTerm));
    
            // Wait for all of the terms to be calculated.
            WaitHandle.WaitAll(autoEvents);
    
            // Reset the wait handle for the next calculation.
            manualEvent.Reset();
    
            return firstTerm + secondTerm + thirdTerm;
        }
    }
    

      

    继承层次结构


    System.Object 
       System.MarshalByRefObject 
         System.Threading.WaitHandle 
           System.Threading.EventWaitHandle 
            System.Threading.ManualResetEvent

    此类是线程安全的。

    Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

    .NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

    .NET Framework

    受以下版本支持:2.0、1.1、1.0

    .NET Compact Framework

    受以下版本支持:2.0、1.0

  • 相关阅读:
    Kubernetes Python Client 初体验之安装授权
    用Ubuntu和树莓派系统部署kubernetes集群后的一些心得
    OpenWrt下基于OLSR的Ad-Hoc组网实现网络摄像头多节点访问
    KVM初体验之virt-manager unable to connect to libvirt的处理办法
    oracle参数和日志文件
    trigger
    package
    function
    procedure
    oracle异常处理
  • 原文地址:https://www.cnblogs.com/YYi_H/p/2169201.html
Copyright © 2011-2022 走看看