Mutex 类

定义

还可用于进程间同步的同步基元。A synchronization primitive that can also be used for interprocess synchronization.

public ref class Mutex sealed : System::Threading::WaitHandle
public sealed class Mutex : System.Threading.WaitHandle
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class Mutex : System.Threading.WaitHandle
type Mutex = class
    inherit WaitHandle
Public NotInheritable Class Mutex
Inherits WaitHandle
继承
继承
属性

示例

此示例演示如何使用本地 Mutex 对象同步对受保护资源的访问权限。This example shows how a local Mutex object is used to synchronize access to a protected resource. 由于每个调用线程将被阻止,直到它获取互斥体的所有权,因此它必须调用 ReleaseMutex 方法才能释放该线程的所有权。Because each calling thread is blocked until it acquires ownership of the mutex, it must call the ReleaseMutex method to release ownership of the thread.

using System;
using System.Threading;

class Example
{
    // Create a new Mutex. The creating thread does not own the mutex.
    private static Mutex mut = new Mutex();
    private const int numIterations = 1;
    private const int numThreads = 3;

    static void Main()
    {
        // Create the threads that will use the protected resource.
        for(int i = 0; i < numThreads; i++)
        {
            Thread newThread = new Thread(new ThreadStart(ThreadProc));
            newThread.Name = String.Format("Thread{0}", i + 1);
            newThread.Start();
        }

        // The main thread exits, but the application continues to
        // run until all foreground threads have exited.
    }

    private static void ThreadProc()
    {
        for(int i = 0; i < numIterations; i++)
        {
            UseResource();
        }
    }

    // This method represents a resource that must be synchronized
    // so that only one thread at a time can enter.
    private static void UseResource()
    {
        // Wait until it is safe to enter.
        Console.WriteLine("{0} is requesting the mutex", 
                          Thread.CurrentThread.Name);
        mut.WaitOne();

        Console.WriteLine("{0} has entered the protected area", 
                          Thread.CurrentThread.Name);

        // Place code to access non-reentrant resources here.

        // Simulate some work.
        Thread.Sleep(500);

        Console.WriteLine("{0} is leaving the protected area", 
            Thread.CurrentThread.Name);

        // Release the Mutex.
        mut.ReleaseMutex();
        Console.WriteLine("{0} has released the mutex", 
            Thread.CurrentThread.Name);
    }
}
// The example displays output like the following:
//       Thread1 is requesting the mutex
//       Thread2 is requesting the mutex
//       Thread1 has entered the protected area
//       Thread3 is requesting the mutex
//       Thread1 is leaving the protected area
//       Thread1 has released the mutex
//       Thread3 has entered the protected area
//       Thread3 is leaving the protected area
//       Thread3 has released the mutex
//       Thread2 has entered the protected area
//       Thread2 is leaving the protected area
//       Thread2 has released the mutex
Imports System.Threading

Module Example
   ' Create a new Mutex. The creating thread does not own the mutex.
   Private mut As New Mutex()
   Private Const numIterations As Integer = 1
   Private Const numThreads As Integer = 3
   
   Public Sub Main()
        ' Create the threads that will use the protected resource.
        For i As Integer = 0 To numThreads - 1
            Dim newThread As New Thread(AddressOf ThreadProc)
            newThread.Name = String.Format("Thread{0}", i + 1)
            newThread.Start()
        Next

        ' The main thread exits, but the application continues to
        ' run until all foreground threads have exited.
    End Sub

    Private Sub ThreadProc()
        For i As Integer = 0 To numIterations - 1
            UseResource()
        Next
    End Sub

    ' This method represents a resource that must be synchronized
    ' so that only one thread at a time can enter.
    Private Sub UseResource()
        ' Wait until it is safe to enter.
        Console.WriteLine("{0} is requesting the mutex", 
                          Thread.CurrentThread.Name)
        mut.WaitOne()

        Console.WriteLine("{0} has entered the protected area", 
                          Thread.CurrentThread.Name)

        ' Place code to access non-reentrant resources here.

        ' Simulate some work.
        Thread.Sleep(500)

        Console.WriteLine("{0} is leaving the protected area", 
            Thread.CurrentThread.Name)

        ' Release the Mutex.
        mut.ReleaseMutex()
        Console.WriteLine("{0} has released the mutex", 
            Thread.CurrentThread.Name)
   End Sub
End Module
' The example displays output like the following:
'       Thread1 is requesting the mutex
'       Thread2 is requesting the mutex
'       Thread1 has entered the protected area
'       Thread3 is requesting the mutex
'       Thread1 is leaving the protected area
'       Thread1 has released the mutex
'       Thread3 has entered the protected area
'       Thread3 is leaving the protected area
'       Thread3 has released the mutex
'       Thread2 has entered the protected area
'       Thread2 is leaving the protected area
'       Thread2 has released the mutex

在下面的示例中,每个线程都调用 WaitOne(Int32) 方法来获取互斥体。In the following example, each thread calls the WaitOne(Int32) method to acquire the mutex. 如果超时间隔已过,则该方法将返回 false,并且该线程既不会获取互斥体,也不会获得对互斥体保护的资源的访问权限。If the time-out interval elapses, the method returns false, and the thread neither acquires the mutex nor gains access to the resource the mutex protects. 仅通过获取互斥体的线程调用 ReleaseMutex 方法。The ReleaseMutex method is called only by the thread that acquires the mutex.

using System;
using System.Threading;

class Example
{
    // Create a new Mutex. The creating thread does not own the mutex.
    private static Mutex mut = new Mutex();
    private const int numIterations = 1;
    private const int numThreads = 3;

    static void Main()
    {
        Example ex = new Example();
        ex.StartThreads();
    }

     private void StartThreads()
     {
        // Create the threads that will use the protected resource.
        for(int i = 0; i < numThreads; i++)
        {
            Thread newThread = new Thread(new ThreadStart(ThreadProc));
            newThread.Name = String.Format("Thread{0}", i + 1);
            newThread.Start();
        }

        // The main thread returns to Main and exits, but the application continues to
        // run until all foreground threads have exited.
    }

    private static void ThreadProc()
    {
        for(int i = 0; i < numIterations; i++)
        {
            UseResource();
        }
    }

    // This method represents a resource that must be synchronized
    // so that only one thread at a time can enter.
    private static void UseResource()
    {
        // Wait until it is safe to enter, and do not enter if the request times out.
        Console.WriteLine("{0} is requesting the mutex", Thread.CurrentThread.Name);
        if (mut.WaitOne(1000)) {
           Console.WriteLine("{0} has entered the protected area", 
               Thread.CurrentThread.Name);
   
           // Place code to access non-reentrant resources here.
   
           // Simulate some work.
           Thread.Sleep(5000);
   
           Console.WriteLine("{0} is leaving the protected area", 
               Thread.CurrentThread.Name);
   
           // Release the Mutex.
              mut.ReleaseMutex();
           Console.WriteLine("{0} has released the mutex", 
                             Thread.CurrentThread.Name);
        }
        else {
           Console.WriteLine("{0} will not acquire the mutex", 
                             Thread.CurrentThread.Name);
        }
    }

    ~Example()
    {
       mut.Dispose();
    }
}
// The example displays output like the following:
//       Thread1 is requesting the mutex
//       Thread1 has entered the protected area
//       Thread2 is requesting the mutex
//       Thread3 is requesting the mutex
//       Thread2 will not acquire the mutex
//       Thread3 will not acquire the mutex
//       Thread1 is leaving the protected area
//       Thread1 has released the mutex
Imports System.Threading

Class Example
   ' Create a new Mutex. The creating thread does not own the mutex.
   Private mut As New Mutex()
   Private Const numIterations As Integer = 1
   Private Const numThreads As Integer = 3

   Public Shared Sub Main()
      Dim ex As New Example()
      ex.StartThreads()
   End Sub
   
   Private Sub StartThreads()
        ' Create the threads that will use the protected resource.
        For i As Integer = 0 To numThreads - 1
            Dim newThread As New Thread(AddressOf ThreadProc)
            newThread.Name = String.Format("Thread{0}", i + 1)
            newThread.Start()
        Next

        ' The main thread returns to Main and exits, but the application continues to
        ' run until all foreground threads have exited.
   End Sub

   Private Sub ThreadProc()
        For i As Integer = 0 To numIterations - 1
            UseResource()
        Next
   End Sub

   ' This method represents a resource that must be synchronized
   ' so that only one thread at a time can enter.
   Private Sub UseResource()
        ' Wait until it is safe to enter.
        Console.WriteLine("{0} is requesting the mutex", 
                          Thread.CurrentThread.Name)
        If mut.WaitOne(1000) Then
           Console.WriteLine("{0} has entered the protected area", 
               Thread.CurrentThread.Name)
   
           ' Place code to access non-reentrant resources here.
   
           ' Simulate some work.
           Thread.Sleep(5000)
   
           Console.WriteLine("{0} is leaving the protected area", 
               Thread.CurrentThread.Name)
   
           ' Release the Mutex.
           mut.ReleaseMutex()
           Console.WriteLine("{0} has released the mutex", 
                             Thread.CurrentThread.Name)
        Else
           Console.WriteLine("{0} will not acquire the mutex", 
                             Thread.CurrentThread.Name)
        End If
   End Sub
   
   Protected Overrides Sub Finalize()
      mut.Dispose()
   End Sub
End Class
' The example displays output like the following:
'       Thread1 is requesting the mutex
'       Thread1 has entered the protected area
'       Thread2 is requesting the mutex
'       Thread3 is requesting the mutex
'       Thread2 will not acquire the mutex
'       Thread3 will not acquire the mutex
'       Thread1 is leaving the protected area
'       Thread1 has released the mutex

注解

当两个或多个线程同时访问共享资源时,系统需要一个同步机制来确保每次只有一个线程使用资源。When two or more threads need to access a shared resource at the same time, the system needs a synchronization mechanism to ensure that only one thread at a time uses the resource. Mutex 是一种同步基元,该基元仅向一个线程授予对共享资源的独占访问权限。Mutex is a synchronization primitive that grants exclusive access to the shared resource to only one thread. 如果线程获取互斥体,则需要获取该互斥体的第二个线程将挂起,直到第一个线程释放该互斥体。If a thread acquires a mutex, the second thread that wants to acquire that mutex is suspended until the first thread releases the mutex.

重要

此类型实现 IDisposable 接口。This type implements the IDisposable interface. 在使用完类型后,您应直接或间接释放类型。When you have finished using the type, you should dispose of it either directly or indirectly. 若要直接释放类型,请在 Disposetry/ 块中调用其 catch 方法。To dispose of the type directly, call its Dispose method in a try/catch block. 若要间接释放类型,请使用 using(在 C# 中)或 Using(在 Visual Basic 中)等语言构造。To dispose of it indirectly, use a language construct such as using (in C#) or Using (in Visual Basic). 有关详细信息,请参阅 IDisposable 接口主题中的“使用实现 IDisposable 的对象”一节。For more information, see the "Using an Object that Implements IDisposable" section in the IDisposable interface topic.

您可以使用 WaitHandle.WaitOne 方法请求 mutex 的所有权。You can use the WaitHandle.WaitOne method to request ownership of a mutex. 调用线程将会阻塞,直到发生以下情况之一:The calling thread blocks until one of the following occurs:

  • 向互斥体发出信号,指示该互斥体不为所有。The mutex is signaled to indicate that it is not owned. 发生这种情况时,WaitOne 方法返回 true,并且调用线程将假定 mutex 的所有权并访问由 mutex 保护的资源。When this happens, the WaitOne method returns true, and the calling thread assumes ownership of the mutex and accesses the resource protected by the mutex. 完成访问资源后,线程必须调用 ReleaseMutex 方法才能释放互斥体的所有权。When it has finished accessing the resource, the thread must call the ReleaseMutex method to release ownership of the mutex. "示例" 部分中的第一个示例说明了此模式。The first example in the Examples section illustrates this pattern.

  • 对具有 millisecondsTimeouttimeout 参数的 WaitOne 方法的调用中指定的超时间隔已过。The time-out interval specified in the call to a WaitOne method that has a millisecondsTimeout or timeout parameter has elapsed. 发生这种情况时,WaitOne 方法返回 false,调用线程不会进一步尝试获取互斥体的所有权。When this happens, the WaitOne method returns false, and the calling thread makes no further attempt to acquire ownership of the mutex. 在这种情况下,您应该构建代码的结构,以便对调用线程拒绝对由 mutex 保护的资源的访问。In this case, you should structure your code so that access to the resource that is protected by the mutex is denied to the calling thread. 由于线程从未获取互斥体的所有权,因此不能调用 ReleaseMutex 方法。Because the thread never acquired ownership of the mutex, it must not call the ReleaseMutex method. "示例" 部分中的第二个示例演示了此模式。The second example in the Examples section illustrates this pattern.

Mutex 类强制执行线程标识,因此互斥体只能由获取它的线程释放。The Mutex class enforces thread identity, so a mutex can be released only by the thread that acquired it. 与此相反,Semaphore 类不强制执行线程标识。By contrast, the Semaphore class does not enforce thread identity. 还可以跨应用程序域边界传递互斥体。A mutex can also be passed across application domain boundaries.

拥有 mutex 的线程可以在对 WaitOne 的重复调用中请求同一互斥体,而不会阻止其执行。The thread that owns a mutex can request the same mutex in repeated calls to WaitOne without blocking its execution. 但是,线程必须调用 ReleaseMutex 方法,以释放互斥体的所有权。However, the thread must call the ReleaseMutex method the same number of times to release ownership of the mutex.

由于 Mutex 类继承自 WaitHandle,因此还可以调用静态 WaitHandle.WaitAllWaitHandle.WaitAny 方法来同步对受保护资源的访问。Because the Mutex class inherits from WaitHandle, you can also call the static WaitHandle.WaitAll and WaitHandle.WaitAny methods to synchronize access to a protected resource.

如果线程在拥有互斥体时终止,则会被视为放弃互斥体。If a thread terminates while owning a mutex, the mutex is said to be abandoned. Mutex 的状态设置为 "已终止",下一个等待线程将获得所有权。The state of the mutex is set to signaled, and the next waiting thread gets ownership. 从 .NET Framework 的2.0 版开始,将在下一个获取被放弃的互斥体的线程中引发 AbandonedMutexExceptionBeginning in version 2.0 of the .NET Framework, an AbandonedMutexException is thrown in the next thread that acquires the abandoned mutex. 在 .NET Framework 版本2.0 之前,不引发异常。Before version 2.0 of the .NET Framework, no exception was thrown.

注意

被放弃的 mutex 通常表明代码中存在严重错误。An abandoned mutex often indicates a serious error in the code. 当线程在不释放 mutex 的情况下退出时,由 mutex 保护的数据结构可能不是一致的状态。When a thread exits without releasing the mutex, the data structures protected by the mutex might not be in a consistent state. 如果可以验证数据结构的完整性,下一个请求 mutex 所有权的线程就可以处理此异常并继续操作。The next thread to request ownership of the mutex can handle this exception and proceed, if the integrity of the data structures can be verified.

对于系统范围的 mutex,放弃的 mutex 可能指示应用程序已突然终止(例如,通过使用 Windows 任务管理器终止)。In the case of a system-wide mutex, an abandoned mutex might indicate that an application has been terminated abruptly (for example, by using Windows Task Manager).

Mutex 分为两种类型:本地互斥体,即未命名和命名的系统互斥体。Mutexes are of two types: local mutexes, which are unnamed, and named system mutexes. 本地 mutex 仅存在于进程中。A local mutex exists only within your process. 您的进程中的任何线程都可以使用它来引用表示 mutex 的 Mutex 对象。It can be used by any thread in your process that has a reference to the Mutex object that represents the mutex. 每个未命名 Mutex 对象表示单独的本地 mutex。Each unnamed Mutex object represents a separate local mutex.

命名系统 mutex 在整个操作系统中均可见,可用于同步进程的活动。Named system mutexes are visible throughout the operating system, and can be used to synchronize the activities of processes. 您可以使用接受名称的构造函数创建表示已命名系统 mutex 的 Mutex 对象。You can create a Mutex object that represents a named system mutex by using a constructor that accepts a name. 可以同时创建操作系统对象,也可以在创建 Mutex 对象之前存在该对象。The operating-system object can be created at the same time, or it can exist before the creation of the Mutex object. 可以创建多个表示同一命名系统 mutex 的 Mutex 对象,还能使用 OpenExisting 方法打开现有的命名系统 mutex。You can create multiple Mutex objects that represent the same named system mutex, and you can use the OpenExisting method to open an existing named system mutex.

备注

在运行终端服务的服务器上,命名系统 mutex 可以有两个级别的可见性。On a server that is running Terminal Services, a named system mutex can have two levels of visibility. 如果其名称以前缀 "Global\" 开头,则 mutex 在所有终端服务器会话中可见。If its name begins with the prefix "Global\", the mutex is visible in all terminal server sessions. 如果其名称以前缀 "Local\" 开头,则 mutex 仅在创建它的终端服务器会话中可见。If its name begins with the prefix "Local\", the mutex is visible only in the terminal server session where it was created. 在这种情况下,可以在服务器上的其他每个终端服务器会话中存在具有相同名称的单独 mutex。In that case, a separate mutex with the same name can exist in each of the other terminal server sessions on the server. 如果在创建已命名的 mutex 时未指定前缀,则采用前缀 "Local\"。If you do not specify a prefix when you create a named mutex, it takes the prefix "Local\". 在终端服务器会话中,两个互斥体的名称只是它们的前缀不同,它们都是对终端服务器会话中的所有进程都可见。Within a terminal server session, two mutexes whose names differ only by their prefixes are separate mutexes, and both are visible to all processes in the terminal server session. 也就是说,前缀名称 "Global\" 和 "Local\" 描述互斥体名称相对于终端服务器会话的作用域,而不是相对于进程。That is, the prefix names "Global\" and "Local\" describe the scope of the mutex name relative to terminal server sessions, not relative to processes.

反斜杠 (\) 是互斥体名称中的保留字符。The backslash (\) is a reserved character in a mutex name. 请勿在互斥体名称中使用反斜杠 (\),除非在终端服务器会话中使用互斥体的注释中另有规定。Don't use a backslash (\) in a mutex name except as specified in the note on using mutexes in terminal server sessions. 否则,即使互斥体的名称表示现有文件,也可能引发 DirectoryNotFoundExceptionOtherwise, a DirectoryNotFoundException may be thrown, even though the name of the mutex represents an existing file.

构造函数

Mutex()

使用默认属性初始化 Mutex 类的新实例。Initializes a new instance of the Mutex class with default properties.

Mutex(Boolean)

使用 Boolean 值(指示调用线程是否应具有互斥体的初始所有权)初始化 Mutex 类的新实例。Initializes a new instance of the Mutex class with a Boolean value that indicates whether the calling thread should have initial ownership of the mutex.

Mutex(Boolean, String)

使用 Boolean 值(指示调用线程是否应具有互斥体的初始所有权以及字符串是否为互斥体的名称)初始化 Mutex 类的新实例。Initializes a new instance of the Mutex class with a Boolean value that indicates whether the calling thread should have initial ownership of the mutex, and a string that is the name of the mutex.

Mutex(Boolean, String, Boolean)

使用可指示调用线程是否应具有互斥体的初始所有权以及字符串是否为互斥体的名称的 Boolean 值和当线程返回时可指示调用线程是否已赋予互斥体的初始所有权的 Boolean 值初始化 Mutex 类的新实例。Initializes a new instance of the Mutex class with a Boolean value that indicates whether the calling thread should have initial ownership of the mutex, a string that is the name of the mutex, and a Boolean value that, when the method returns, indicates whether the calling thread was granted initial ownership of the mutex.

Mutex(Boolean, String, Boolean, MutexSecurity)

使用可指示调用线程是否应具有互斥体的初始所有权以及字符串是否为互斥体的名称的 Boolean 值和当线程返回时可指示调用线程是否已赋予互斥体的初始所有权以及访问控制安全是否已应用到命名互斥体的 Boolean 变量初始化 Mutex 类的新实例。Initializes a new instance of the Mutex class with a Boolean value that indicates whether the calling thread should have initial ownership of the mutex, a string that is the name of the mutex, a Boolean variable that, when the method returns, indicates whether the calling thread was granted initial ownership of the mutex, and the access control security to be applied to the named mutex.

字段

WaitTimeout

指示在任何等待句柄终止之前 WaitAny(WaitHandle[], Int32, Boolean) 操作已超时。Indicates that a WaitAny(WaitHandle[], Int32, Boolean) operation timed out before any of the wait handles were signaled. 此字段是常量。This field is constant.

(继承自 WaitHandle)

属性

Handle

获取或设置本机操作系统句柄。Gets or sets the native operating system handle.

(继承自 WaitHandle)
SafeWaitHandle

获取或设置本机操作系统句柄。Gets or sets the native operating system handle.

(继承自 WaitHandle)

方法

Close()

释放由当前 WaitHandle 占用的所有资源。Releases all resources held by the current WaitHandle.

(继承自 WaitHandle)
CreateObjRef(Type)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(继承自 MarshalByRefObject)
Dispose()

释放由 WaitHandle 类的当前实例占用的所有资源。Releases all resources used by the current instance of the WaitHandle class.

(继承自 WaitHandle)
Dispose(Boolean)

当在派生类中重写时,释放 WaitHandle 使用的非托管资源,并且可选择释放托管资源。When overridden in a derived class, releases the unmanaged resources used by the WaitHandle, and optionally releases the managed resources.

(继承自 WaitHandle)
Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetAccessControl()

获取表示已命名的互斥体的访问控制安全性的 MutexSecurity 对象。Gets a MutexSecurity object that represents the access control security for the named mutex.

GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetLifetimeService()

检索控制此实例的生存期策略的当前生存期服务对象。Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(继承自 MarshalByRefObject)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
InitializeLifetimeService()

获取生存期服务对象来控制此实例的生存期策略。Obtains a lifetime service object to control the lifetime policy for this instance.

(继承自 MarshalByRefObject)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
MemberwiseClone(Boolean)

创建当前 MarshalByRefObject 对象的浅表副本。Creates a shallow copy of the current MarshalByRefObject object.

(继承自 MarshalByRefObject)
OpenExisting(String)

打开指定的已命名的互斥体(如果已经存在)。Opens the specified named mutex, if it already exists.

OpenExisting(String, MutexRights)

利用所需的安全访问权限,打开指定的已命名的互斥体(如果已经存在)。Opens the specified named mutex, if it already exists, with the desired security access.

ReleaseMutex()

释放 Mutex 一次。Releases the Mutex once.

SetAccessControl(MutexSecurity)

设置已命名的系统互斥体的访问控制安全性。Sets the access control security for a named system mutex.

ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)
TryOpenExisting(String, Mutex)

打开指定的已命名的互斥体(如果已经存在),并返回指示操作是否成功的值。Opens the specified named mutex, if it already exists, and returns a value that indicates whether the operation succeeded.

TryOpenExisting(String, MutexRights, Mutex)

利用所需的安全访问权限,打开指定的已命名的互斥体(如果已经存在),并返回指示操作是否成功的值。Opens the specified named mutex, if it already exists, with the desired security access, and returns a value that indicates whether the operation succeeded.

WaitOne()

阻止当前线程,直到当前 WaitHandle 收到信号。Blocks the current thread until the current WaitHandle receives a signal.

(继承自 WaitHandle)
WaitOne(Int32)

阻止当前线程,直到当前 WaitHandle 收到信号,同时使用 32 位带符号整数指定时间间隔(以毫秒为单位)。Blocks the current thread until the current WaitHandle receives a signal, using a 32-bit signed integer to specify the time interval in milliseconds.

(继承自 WaitHandle)
WaitOne(Int32, Boolean)

阻止当前线程,直到当前的 WaitHandle 收到信号为止,同时使用 32 位带符号整数指定时间间隔,并指定是否在等待之前退出同步域。Blocks the current thread until the current WaitHandle receives a signal, using a 32-bit signed integer to specify the time interval and specifying whether to exit the synchronization domain before the wait.

(继承自 WaitHandle)
WaitOne(TimeSpan)

阻止当前线程,直到当前实例收到信号,同时使用 TimeSpan 指定时间间隔。Blocks the current thread until the current instance receives a signal, using a TimeSpan to specify the time interval.

(继承自 WaitHandle)
WaitOne(TimeSpan, Boolean)

阻止当前线程,直到当前实例收到信号为止,同时使用 TimeSpan 指定时间间隔,并指定是否在等待之前退出同步域。Blocks the current thread until the current instance receives a signal, using a TimeSpan to specify the time interval and specifying whether to exit the synchronization domain before the wait.

(继承自 WaitHandle)

显式接口实现

IDisposable.Dispose()

释放 WaitHandle 使用的所有资源。Releases all resources used by the WaitHandle.

(继承自 WaitHandle)

扩展方法

GetAccessControl(Mutex)
SetAccessControl(Mutex, MutexSecurity)
GetSafeWaitHandle(WaitHandle)

获取本机操作系统等待句柄的安全句柄。Gets the safe handle for a native operating system wait handle.

SetSafeWaitHandle(WaitHandle, SafeWaitHandle)

设置本机操作系统等待句柄的安全句柄。Sets a safe handle for a native operating system wait handle.

适用于

线程安全性

此类型是线程安全的。This type is thread safe.

另请参阅