Mutex Mutex Mutex Mutex Class

定义

一个同步基元,也可用于进程间同步。A synchronization primitive that can also be used for interprocess synchronization.

public ref class Mutex sealed : 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. 因为每个调用线程被阻止,直到它获取 mutex 的所有权,因此它必须调用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,并在线程获取 mutex 既不获得互斥锁保护的资源访问权。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. 若要直接释放类型,调用其Dispose中的方法try / 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来请求所有权的互斥体的方法。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,调用线程是互斥体的所有权,并访问受该互斥体的资源。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方法来释放 mutex 的所有权。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.

  • 对的调用中指定的超时间隔WaitOne具有方法millisecondsTimeouttimeout参数已过。The time-out interval specified in the call to a WaitOne method that has a millisecondsTimeout or timeout parameter has elapsed. 在此情况下,WaitOne方法将返回false,并调用线程不会进一步尝试获取 mutex 的所有权。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. 线程永远不会获取 mutex 的所有权,因为它不能调用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方法相同数量的次数与释放 mutex 的所有权。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.

如果线程终止时拥有互斥体,则认为该 mutex 已放弃。If a thread terminates while owning a mutex, the mutex is said to be abandoned. 互斥体的状态设置为终止状态,并且下一个等待线程获取所有权。The state of the mutex is set to signaled, and the next waiting thread gets ownership. 从.NET Framework 的版本 2.0AbandonedMutexException获取已放弃的互斥体的下一个线程中引发。Beginning 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 保护的数据结构不可能处于一致状态。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 有两种类型: 本地 mutex,未命名,并命名系统 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表示该互斥体的对象。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.

已命名的系统互斥体,显示整个操作系统,可用于同步进程的活动。Named system mutexes are visible throughout the operating system, and can be used to synchronize the activities of processes. 您可以创建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.

备注

在运行终端服务的服务器,命名的系统互斥体可以包含两个级别的可见性。On a server that is running Terminal Services, a named system mutex can have two levels of visibility. 如果其名称以前缀开头"Global\",互斥体是在所有终端服务器会话中可见。If its name begins with the prefix "Global\", the mutex is visible in all terminal server sessions. 如果其名称以前缀开头"本地\",互斥体是仅在终端服务器会话中可见的创建位置。If its name begins with the prefix "Local\", the mutex is visible only in the terminal server session where it was created. 在这种情况下,具有相同名称单独的互斥锁可以存在于每个服务器上的其他终端服务器会话。In that case, a separate mutex with the same name can exist in each of the other terminal server sessions on the server. 如果您不指定前缀创建已命名的互斥体时,它将前缀"本地\"。If you do not specify a prefix when you create a named mutex, it takes the prefix "Local\". 终端服务器会话中,只能由其前缀的名称不同的两个 mutex 有单独的互斥体,且都在终端服务器会话中对所有进程可见。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\"和"本地\"描述相对于终端服务器会话、 不相对于进程的互斥体名称的作用域。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() Mutex() Mutex()

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

Mutex(Boolean) Mutex(Boolean) Mutex(Boolean) Mutex(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) Mutex(Boolean, String) Mutex(Boolean, String) Mutex(Boolean, String)

用一个指示调用线程是否应拥有互斥体的初始所属权的布尔值和一个作为互斥体名称的字符串来初始化 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) Mutex(Boolean, String, Boolean) Mutex(Boolean, String, Boolean) 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) Mutex(Boolean, String, Boolean, MutexSecurity) Mutex(Boolean, String, Boolean, MutexSecurity) 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.

方法

Close() Close() Close() Close()

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

(Inherited from WaitHandle)
CreateObjRef(Type) CreateObjRef(Type) CreateObjRef(Type) CreateObjRef(Type)

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

(Inherited from MarshalByRefObject)
Dispose() Dispose() Dispose() Dispose()

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

(Inherited from WaitHandle)
Dispose(Boolean) Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

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

(Inherited from WaitHandle)
Equals(Object) Equals(Object) Equals(Object) Equals(Object)

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

(Inherited from Object)
GetAccessControl() GetAccessControl() GetAccessControl() GetAccessControl()

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

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

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

(Inherited from Object)
GetLifetimeService() GetLifetimeService() GetLifetimeService() GetLifetimeService()

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

(Inherited from MarshalByRefObject)
GetType() GetType() GetType() GetType()

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

(Inherited from Object)
InitializeLifetimeService() InitializeLifetimeService() InitializeLifetimeService() InitializeLifetimeService()

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

(Inherited from MarshalByRefObject)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

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

(Inherited from Object)
MemberwiseClone(Boolean) MemberwiseClone(Boolean) MemberwiseClone(Boolean) MemberwiseClone(Boolean)

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

(Inherited from MarshalByRefObject)
OpenExisting(String) OpenExisting(String) OpenExisting(String) OpenExisting(String)

打开指定名称为 mutex(如果已经存在)。Opens the specified named mutex, if it already exists.

OpenExisting(String, MutexRights) OpenExisting(String, MutexRights) OpenExisting(String, MutexRights) OpenExisting(String, MutexRights)

用安全访问权限打开指定名称为 mutex(如果已经存在),并返回指示操作是否成功的值。Opens the specified named mutex, if it already exists, with the desired security access.

ReleaseMutex() ReleaseMutex() ReleaseMutex() ReleaseMutex()

释放 Mutex 一次。Releases the Mutex once.

SetAccessControl(MutexSecurity) SetAccessControl(MutexSecurity) SetAccessControl(MutexSecurity) SetAccessControl(MutexSecurity)

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

ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)
TryOpenExisting(String, Mutex) TryOpenExisting(String, Mutex) TryOpenExisting(String, Mutex) 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) TryOpenExisting(String, MutexRights, Mutex) TryOpenExisting(String, MutexRights, Mutex) 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() WaitOne() WaitOne() WaitOne()

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

(Inherited from WaitHandle)
WaitOne(Int32) WaitOne(Int32) WaitOne(Int32) 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.

(Inherited from WaitHandle)
WaitOne(Int32, Boolean) WaitOne(Int32, Boolean) WaitOne(Int32, Boolean) 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.

(Inherited from WaitHandle)
WaitOne(TimeSpan) WaitOne(TimeSpan) WaitOne(TimeSpan) WaitOne(TimeSpan)

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

(Inherited from WaitHandle)
WaitOne(TimeSpan, Boolean) WaitOne(TimeSpan, Boolean) WaitOne(TimeSpan, Boolean) 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.

(Inherited from WaitHandle)

属性

Handle Handle Handle Handle

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

(Inherited from WaitHandle)
SafeWaitHandle SafeWaitHandle SafeWaitHandle SafeWaitHandle

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

(Inherited from WaitHandle)

显式界面实现

IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose()

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

(Inherited from WaitHandle)

字段

WaitTimeout WaitTimeout WaitTimeout 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.

(Inherited from WaitHandle)

扩展方法

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

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

SetSafeWaitHandle(WaitHandle, SafeWaitHandle) SetSafeWaitHandle(WaitHandle, SafeWaitHandle) SetSafeWaitHandle(WaitHandle, SafeWaitHandle) SetSafeWaitHandle(WaitHandle, SafeWaitHandle)

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

适用于

线程安全性

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

另请参阅