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
[<System.Runtime.InteropServices.ComVisible(true)>]
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 方法來釋放 mutex 的擁有權。Because each calling thread is blocked until it acquires ownership of the mutex, it must call the ReleaseMutex method to release ownership of 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()
    {
        // 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) 方法來取得 mutex。In the following example, each thread calls the WaitOne(Int32) method to acquire the mutex. 如果逾時間隔超過此方法,則方法會傳回 false ,而且執行緒也不會取得 mutex,也不會取得 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方法只會由取得 mutex 的執行緒呼叫。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. 如果執行緒取得 mutex,則想要取得該 mutex 的第二個執行緒會被擱置,直到第一個執行緒釋放 mutex 為止。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. 若要直接處置型別,請呼叫其 try/catch 區塊中的 Dispose 方法。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:

  • Mutex 會收到信號,表示它不是擁有的。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 方法來釋放 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 具有或參數的方法時所指定的逾時間隔 millisecondsTimeout timeoutThe 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類別會強制執行執行緒識別,因此 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. Mutex 也可以跨應用程式域界限傳遞。A mutex can also be passed across application domain boundaries.

擁有 mutex 的執行緒可以在重複呼叫中要求相同的 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 時終止,則會將 mutex 視為被放棄。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 版開始, AbandonedMutexException 會在下一個取得已放棄 mutex 的執行緒中擲回。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 時,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 代表 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. 您可以同時建立作業系統物件,也可以在建立物件之前先將它存在 MutexThe 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\". 在終端機伺服器會話中,有兩個不同的 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 \ " 和 "Local \ " 會描述相對於終端機伺服器會話之 mutex 名稱的範圍,而不是相對於處理常式。That is, the prefix names "Global\" and "Local\" describe the scope of the mutex name relative to terminal server sessions, not relative to processes.

反斜線 (\) 是 Mutex 名稱的保留字元。The backslash (\) is a reserved character in a mutex name. 請勿在 Mutex 名稱中使用反斜線 (\),除非在終端機伺服器工作階段中使用 Mutex 的注意事項中有指定。Don't use a backslash (\) in a mutex name except as specified in the note on using mutexes in terminal server sessions. 否則,可能會擲回 DirectoryNotFoundException,即使是 Mutex 的名稱代表現有的檔案。Otherwise, 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 類別的新執行個體,指出呼叫執行緒是否應該具有 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 類別的新執行個體,指出呼叫執行緒是否應該具有 Mutex 的初始擁有權,並擁有為 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 的初始擁有權的布林值、代表 Mutex 名稱的字串,以及當方法傳回時表示是否將 Mutex 的初始擁有權授與呼叫執行緒的布林值,初始化 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 的初始擁有權的布林值、代表 Mutex 名稱的字串、當這個方法傳回時表示是否將 Mutex 的初始擁有權授與呼叫執行緒的布林值,以及要套用至具名 Mutex 的存取控制安全性,初始化 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
已過時。

取得或設定原生 (Native) 的作業系統控制代碼。Gets or sets the native operating system handle.

(繼承來源 WaitHandle)
SafeWaitHandle

取得或設定原生 (Native) 的作業系統控制代碼。Gets or sets the native operating system handle.

(繼承來源 WaitHandle)

方法

Close()

釋放目前 WaitHandle 所持有的全部資源。Releases all resources held by the current WaitHandle.

(繼承來源 WaitHandle)
CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。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 所使用的 Unmanaged 資源,並選擇性釋放 Managed 資源。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 物件,表示具名 Mutex 的存取控制安全性。Gets a MutexSecurity object that represents the access control security for the named mutex.

GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetLifetimeService()
已過時。

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。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)

開啟指定的具名 mutex (如果已經存在)。Opens the specified named mutex, if it already exists.

OpenExisting(String, MutexRights)

使用所需的安全性存取權,開啟指定的具名 mutex (如果已經存在)。Opens the specified named mutex, if it already exists, with the desired security access.

ReleaseMutex()

釋出 Mutex 一次。Releases the Mutex once.

SetAccessControl(MutexSecurity)

為具名系統 Mutex 設定存取控制安全性。Sets the access control security for a named system mutex.

ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)
TryOpenExisting(String, Mutex)

開啟指定的具名 mutex (如果已經存在),並傳回值,指出作業是否成功。Opens the specified named mutex, if it already exists, and returns a value that indicates whether the operation succeeded.

TryOpenExisting(String, MutexRights, Mutex)

使用所需的安全性存取權,開啟指定的具名 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()

此 API 支援此產品基礎結構,但無法直接用於程式碼之中。

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

(繼承來源 WaitHandle)

擴充方法

GetAccessControl(Mutex)

傳回所指定 mutex 的安全性描述元。Returns the security descriptors for the specified mutex.

SetAccessControl(Mutex, MutexSecurity)

設定所指定 mutex 的安全性描述元。Sets the security descriptors for the specified mutex.

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.

另請參閱