MutexMutexes

您可以使用 Mutex 物件來提供獨佔的資源存取權。You can use a Mutex object to provide exclusive access to a resource. Mutex 類別所使用的系統資源比 Monitor 類別還多,但前者可跨越應用程式定義域的界限進行封送處理、可搭配多個等候使用,並可用來同步處理不同處理序內的執行緒。The Mutex class uses more system resources than the Monitor class, but it can be marshaled across application domain boundaries, it can be used with multiple waits, and it can be used to synchronize threads in different processes. 如需受管理之同步處理機制的比較,請參閱同步處理原始物件概觀For a comparison of managed synchronization mechanisms, see Overview of Synchronization Primitives.

如需程式碼範例,請參閱 Mutex 建構函式的參考文件。For code examples, see the reference documentation for the Mutex constructors.

使用 MutexUsing Mutexes

執行緒會呼叫 Mutex 的 WaitOne 方法來要求擁有權。A thread calls the WaitOne method of a mutex to request ownership. 該呼叫會封鎖起來,直到 Mutex 可供使用或直到選擇性的逾時間隔時間過去。The call blocks until the mutex is available, or until the optional timeout interval elapses. 如果沒有任何執行緒擁有 Mutex,則 Mutex 的狀態為已發出訊號。The state of a mutex is signaled if no thread owns it.

執行緒會藉由呼叫 Mutex 的 ReleaseMutex 方法來釋放它。A thread releases a mutex by calling its ReleaseMutex method. Mutex 具有執行緒相似性;也就是說,Mutex 的釋放只能由擁有該 Mutex 的執行緒來執行。Mutexes have thread affinity; that is, the mutex can be released only by the thread that owns it. 如果執行緒釋放非它擁有的 Mutex,執行緒中就會擲回 ApplicationExceptionIf a thread releases a mutex it does not own, an ApplicationException is thrown in the thread.

因為 Mutex 類別衍生自 WaitHandle,您也可以呼叫 WaitHandle 的靜態 WaitAllWaitAny 方法,搭配其他等候控制代碼來要求 Mutex 的擁有權。Because the Mutex class derives from WaitHandle, you can also call the static WaitAll or WaitAny methods of WaitHandle to request ownership of a Mutex in combination with other wait handles.

如果執行緒擁有 Mutex,該執行緒就能在重複的等候要求呼叫中指定相同的 Mutex,而不需封鎖其執行;但是,它也必須進行相同次數的 Mutex 釋放作業,以釋放擁有權。If a thread owns a Mutex, that thread can specify the same Mutex in repeated wait-request calls without blocking its execution; however, it must release the Mutex as many times to release ownership.

遭到放棄的 MutexAbandoned Mutexes

如果執行緒終止時未釋放 Mutex,就表示已放棄 Mutex。If a thread terminates without releasing a Mutex, the mutex is said to be abandoned. 這通常代表程式在設計上有嚴重錯誤,因為 Mutex 所保護的資源可能仍處於不一致的狀態。This often indicates a serious programming error because the resource the mutex is protecting might be left in an inconsistent state. AbandonedMutexException在下一個取得 mutex 的執行緒中擲回。An AbandonedMutexException is thrown in the next thread that acquires the mutex.

如果是全系統 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).

本機和系統 MutexLocal and System Mutexes

Mutex 有兩種類型︰本機 Mutex 和具名的系統 Mutex。Mutexes are of two types: local mutexes and named system mutexes. 如果您使用可接受名稱的建構函式來建立 Mutex 物件,該物件便會與該名稱的作業系統物件相關聯。If you create a Mutex object using a constructor that accepts a name, it is associated with an operating-system object of that name. 具名的系統 Mutex 能在整個作業系統中看到,而且可用來同步處理處理序的活動。Named system mutexes are visible throughout the operating system and can be used to synchronize the activities of processes. 您可以建立多個 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只存在於您的處理序內。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 local Mutex object. 每個 Mutex 物件都是獨立的本機 Mutex。Each Mutex object is a separate local mutex.

系統 Mutex 的存取控制安全性Access Control Security for System Mutexes

.NET 提供針對命名系統物件查詢和設定 Windows 存取控制安全性的能力。.NET provides the ability to query and set Windows access control security for named system objects. 我們會建議您在建立系統 Mutex 後就為其提供保護,因為系統物件為全域所有,因此非您所擁有的程式碼也可將其鎖定。Protecting system mutexes from the moment of creation is recommended because system objects are global and therefore can be locked by code other than your own.

如需適用於 Mutex 的存取控制安全性相關資訊,請參閱 MutexSecurityMutexAccessRule 類別、MutexRights 列舉、GetAccessControlSetAccessControlMutex 類別的 OpenExisting 方法,以及 Mutex(Boolean, String, Boolean, MutexSecurity) 建構函式。For information on access control security for mutexes, see the MutexSecurity and MutexAccessRule classes, the MutexRights enumeration, the GetAccessControl, SetAccessControl, and OpenExisting methods of the Mutex class, and the Mutex(Boolean, String, Boolean, MutexSecurity) constructor.

另請參閱See also