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)取得 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. 若要直接處置型別,呼叫其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方法來要求擁有權的 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方法具有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類別會強制執行緒身分識別,因此可以釋出 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 的執行緒可以要求在重複呼叫相同的 mutexWaitOne而不會封鎖其執行。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. 可以在此同時,建立作業系統物件,或它可以在建立之前存在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. 如果其名稱開頭為前置詞"本機\",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 時,您不會指定前置詞,它會採用前置詞"本機\"。If you do not specify a prefix when you create a named mutex, it takes the prefix "Local\". 終端機伺服器工作階段中,名稱只能由其前置詞不同的兩個 mutex 有個別的 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\」 和 「 本機\」 描述相對於終端機伺服器工作階段,不是相對於處理程序之 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() Mutex() Mutex()

使用預設屬性,初始化 Mutex 類別的新執行個體。Initializes a new instance of the Mutex class with default properties.

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

方法

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

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

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

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。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 所使用的 Unmanaged 資源,並選擇性釋放 Managed 資源。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 物件,表示具名 Mutex 的存取控制安全性。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()

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

為具名系統 Mutex 設定存取控制安全性。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)

開啟指定的具名 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)

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

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

(Inherited from WaitHandle)
SafeWaitHandle SafeWaitHandle SafeWaitHandle SafeWaitHandle

取得或設定原生 (Native) 的作業系統控制代碼。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)

Extension Methods

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.

另請參閱