Condividi tramite


Mutex Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

A synchronization primitive that can also be used for interprocess synchronization.

Inheritance Hierarchy

System.Object
  System.Threading.WaitHandle
    System.Threading.Mutex

Namespace:  System.Threading
Assembly:  mscorlib.Extensions (in mscorlib.Extensions.dll)

Syntax

'Declaration
Public NotInheritable Class Mutex _
    Inherits WaitHandle
public sealed class Mutex : WaitHandle

The Mutex type exposes the following members.

Constructors

  Name Description
Public methodSupported by Silverlight for Windows Phone 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.

Top

Properties

  Name Description
Public propertySupported by Silverlight for Windows Phone SafeWaitHandle Gets or sets the native operating-system handle. (Inherited from WaitHandle.)

Top

Methods

  Name Description
Public methodSupported by Silverlight for Windows Phone Close When overridden in a derived class, releases all resources held by the current WaitHandle. (Inherited from WaitHandle.)
Public methodSupported by Silverlight for Windows Phone Dispose() Releases all resources used by the current instance of the WaitHandle class. (Inherited from WaitHandle.)
Protected methodSupported by Silverlight for Windows Phone Dispose(Boolean) When overridden in a derived class, releases the unmanaged resources used by the WaitHandle, and optionally releases the managed resources. (Inherited from WaitHandle.)
Public methodSupported by Silverlight for Windows Phone Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows Phone Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone GetType Gets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows Phone MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone ReleaseMutex Releases the Mutex once.
Public methodSupported by Silverlight for Windows Phone ToString Returns a string that represents the current object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone WaitOne() Blocks the current thread until the current Mutex receives a signal. (Overrides WaitHandle.WaitOne().)
Public methodSupported by Silverlight for Windows Phone WaitOne(Int32) Blocks the calling thread for the specified time until the current WaitHandle receives a signal. (Overrides WaitHandle.WaitOne(Int32).)
Public methodSupported by Silverlight for Windows Phone WaitOne(TimeSpan) Blocks the current thread until the current instance receives a signal, using a TimeSpan to specify the time interval. (Inherited from WaitHandle.)

Top

Remarks

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

The Mutex class enforces thread identity, so a mutex can be released only by the thread that acquired it.

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.

Caution noteCaution:

An abandoned mutex often indicates a serious error in the code. When a thread exits without releasing the mutex, the data structures protected by the mutex might not be in a consistent state. 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.

In the case of a system-wide mutex, an abandoned mutex might indicate that an application has been terminated abruptly.

Mutexes are of two types: local mutexes, which are unnamed, and named system mutexes. A local mutex exists only within your process. It can be used by any thread in your process that has a reference to the Mutex object that represents the 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. You can create a Mutex object that represents a named system mutex by using a constructor that accepts a name. The operating-system object can be created at the same time, or it can exist before the creation of the Mutex object. You can create multiple Mutex objects that represent the same named system mutex.

Version Notes

Silverlight for Windows Phone Silverlight for Windows Phone

 Mutex is supported in Silverlight for Windows Phone Only.

Version Information

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Thread Safety

This type is thread safe.

See Also

Reference