本文档已存档,并且将不进行维护。

WaitHandle 类

封装等待对共享资源的独占访问的操作系统特定的对象。

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

[ComVisibleAttribute(true)] 
public abstract class WaitHandle : MarshalByRefObject, IDisposable
/** @attribute ComVisibleAttribute(true) */ 
public abstract class WaitHandle extends MarshalByRefObject implements IDisposable
ComVisibleAttribute(true) 
public abstract class WaitHandle extends MarshalByRefObject implements IDisposable
不适用。

此类通常用作同步对象的基类。从 WaitHandle 派生的类定义一个信号传输机制以指示获取或释放对共享资源的独占访问,但使用继承的 WaitHandle 方法在等待访问共享资源时阻止。

使用此类的静态方法阻塞一个线程,直到一个或多个同步对象接收到信号。

WaitHandle 实现 Dispose 模式。请参见实现 Finalize 和 Dispose 以清理非托管资源。在从 WaitHandle 派生时,使用 SafeWaitHandle 属性可存储本机句柄操作系统句柄。如果不使用其他非托管资源,则无需重写受保护的 Dispose 方法。

下面的代码示例演示当主线程使用 WaitHandle 类的静态 M:System.Threading.WaitHandle.WaitAny(System.Threading.WaitHandle[])WaitAll 方法等待任务完成时,两个线程可以如何完成后台任务。

using System;
using System.Threading;

public sealed class App 
{
    // Define an array with two AutoResetEvent WaitHandles.
    static WaitHandle[] waitHandles = new WaitHandle[] 
    {
        new AutoResetEvent(false),
        new AutoResetEvent(false)
    };

    // Define a random number generator for testing.
    static Random r = new Random();

    static void Main() 
    {
        // Queue up two tasks on two different threads; 
        // wait until all tasks are completed.
        DateTime dt = DateTime.Now;
        Console.WriteLine("Main thread is waiting for BOTH tasks to complete.");
        ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[0]);
        ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[1]);
        WaitHandle.WaitAll(waitHandles);
        // The time shown below should match the longest task.
        Console.WriteLine("Both tasks are completed (time waited={0})", 
            (DateTime.Now - dt).TotalMilliseconds);

        // Queue up two tasks on two different threads; 
        // wait until any tasks are completed.
        dt = DateTime.Now;
        Console.WriteLine();
        Console.WriteLine("The main thread is waiting for either task to complete.");
        ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[0]);
        ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[1]);
        int index = WaitHandle.WaitAny(waitHandles);
        // The time shown below should match the shortest task.
        Console.WriteLine("Task {0} finished first (time waited={1}).",
            index + 1, (DateTime.Now - dt).TotalMilliseconds);
    }

    static void DoTask(Object state) 
    {
        AutoResetEvent are = (AutoResetEvent) state;
        int time = 1000 * r.Next(2, 10);
        Console.WriteLine("Performing a task for {0} milliseconds.", time);
        Thread.Sleep(time);
        are.Set();
    }
}

// This code produces output similar to the following:
//
//  Main thread is waiting for BOTH tasks to complete.
//  Performing a task for 7000 milliseconds.
//  Performing a task for 4000 milliseconds.
//  Both tasks are completed (time waited=7064.8052)
// 
//  The main thread is waiting for either task to complete.
//  Performing a task for 2000 milliseconds.
//  Performing a task for 2000 milliseconds.
//  Task 1 finished first (time waited=2000.6528).

此类型是线程安全的。

Windows 98, Windows Server 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

Windows Vista、Microsoft Windows XP SP2 和 Windows Server 2003 SP1 支持 Microsoft .NET Framework 3.0。

.NET Framework

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

.NET Compact Framework

受以下版本支持:2.0、1.0

XNA Framework

受以下版本支持:1.0
显示: