System.Threading.Mutex Class

Assembly: Mscorlib.dll
Namespace: System.Threading
Summary
A synchronization primitive than can also be used for interprocess synchronization.
C# Syntax:
public sealed class Mutex : WaitHandle
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.

You can use WaitHandle.WaitOne to request ownership of a mutex. The thread that owns a mutex can request the same mutex in repeated calls to Wait without blocking its execution. However, the thread must call the Mutex.ReleaseMutex method the same number of times to release ownership of the mutex. If a thread terminates normally while owning a mutex, the state of the mutex is set to signaled and the next waiting thread gets ownership. If no one owns the mutex, the state of the mutex is signaled.

See also:
System.Threading Namespace | WaitHandle | Thread | MSDN: managedunmanagedthreading

System.Threading.Mutex Member List:

Public Constructors
ctor #1 Overloaded:
.ctor()

Default constructor. This constructor is called by derived class constructors to initialize state in this type.
Initializes a new instance of the Mutex class with default properties.
ctor #2 Overloaded:
.ctor(bool initiallyOwned)

Initializes a new instance of the Mutex class with a Boolean value indicating whether the calling thread should have initial ownership of the mutex.
ctor #3 Overloaded:
.ctor(bool initiallyOwned, string name)

Initializes a new instance of the Mutex class with a Boolean value indicating whether the calling thread should have initial ownership of the mutex, and a string that is the name of the mutex.
ctor #4 Overloaded:
.ctor(bool initiallyOwned, string name, out bool createdNew)

Initializes a new instance of the Mutex class with a Boolean value indicating 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, will indicate whether the calling thread was granted initial ownership of the mutex.
Public Properties
Handle
(inherited from System.Threading.WaitHandle)
Read-write

See base class member description: System.Threading.WaitHandle.Handle


Gets or sets the native operating system handle.
Public Methods
Close
(inherited from System.Threading.WaitHandle)
See base class member description: System.Threading.WaitHandle.Close


When overridden in a derived class, releases all resources held by the current WaitHandle.
CreateObjRef
(inherited from System.MarshalByRefObject)
See base class member description: System.MarshalByRefObject.CreateObjRef


Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetLifetimeService
(inherited from System.MarshalByRefObject)
See base class member description: System.MarshalByRefObject.GetLifetimeService


Retrieves the current lifetime service object that controls the lifetime policy for this instance.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
InitializeLifetimeService
(inherited from System.MarshalByRefObject)
See base class member description: System.MarshalByRefObject.InitializeLifetimeService


Obtains a lifetime service object to control the lifetime policy for this instance.
ReleaseMutex Releases the Mutex once.
ToString
(inherited from System.Object)
See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
WaitOne
(inherited from System.Threading.WaitHandle)
Overloaded:
WaitOne()

See base class member description: System.Threading.WaitHandle.WaitOne


When overridden in a derived class, blocks the current thread until the current WaitHandle receives a signal.
WaitOne
(inherited from System.Threading.WaitHandle)
Overloaded:
WaitOne(int millisecondsTimeout, bool exitContext)

See base class member description: System.Threading.WaitHandle.WaitOne


When overridden in a derived class, blocks the current thread until the current WaitHandle receives a signal, using 32-bit signed integer to measure the time interval and specifying whether to exit the synchronization domain before the wait.
WaitOne
(inherited from System.Threading.WaitHandle)
Overloaded:
WaitOne(TimeSpan timeout, bool exitContext)

See base class member description: System.Threading.WaitHandle.WaitOne


When overridden in a derived class, blocks the current thread until the current instance receives a signal, using a TimeSpan to measure the time interval and specifying whether to exit the synchronization domain before the wait.
Protected Methods
Dispose
(inherited from System.Threading.WaitHandle)
See base class member description: System.Threading.WaitHandle.Dispose


When overridden in a derived class, releases the unmanaged resources used by the WaitHandle, and optionally releases the managed resources.
Finalize
(inherited from System.Threading.WaitHandle)
See base class member description: System.Threading.WaitHandle.Finalize


Releases the resources held by the current instance.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.Threading.Mutex Member Details

Overloaded ctor #1
Summary
Initializes a new instance of the Mutex class with default properties.

Default constructor. This constructor is called by derived class constructors to initialize state in this type.
C# Syntax:
public Mutex();

Return to top


Overloaded ctor #2
Summary
Initializes a new instance of the Mutex class with a Boolean value indicating whether the calling thread should have initial ownership of the mutex.
C# Syntax:
public Mutex(
   bool initiallyOwned
);
Parameters:

initiallyOwned

true to give the calling thread initial ownership of the mutex; otherwise, false.

Return to top


Overloaded ctor #3
Summary
Initializes a new instance of the Mutex class with a Boolean value indicating whether the calling thread should have initial ownership of the mutex, and a string that is the name of the mutex.
C# Syntax:
public Mutex(
   bool initiallyOwned,
   string name
);
Parameters:

initiallyOwned

true to give the calling thread initial ownership of the mutex; otherwise, false.

name

The name of the Mutex. If the value is null, the Mutex is unnamed.

Remarks
If the name is not null and initiallyOwned is true, then the application must ensure that a mutex that has the same name and is owned by the calling thread does not already exist. If the mutex is being used for cross-process communication, you should set initiallyOwned to false, or use the Mutex constructor. Otherwise, it will be difficult to determine which process has initial ownership.

Return to top


Overloaded ctor #4
Summary
Initializes a new instance of the Mutex class with a Boolean value indicating 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, will indicate whether the calling thread was granted initial ownership of the mutex.
C# Syntax:
public Mutex(
   bool initiallyOwned,
   string name,
   out bool createdNew
);
Parameters:

initiallyOwned

true to give the calling thread initial ownership of the mutex; otherwise, false.

name

The name of the Mutex. If the value is null, the Mutex is unnamed.

createdNew

When this method returns, contains a Boolean that is true if the calling thread was granted initial ownership of the mutex; otherwise, false. This parameter is passed uninitialized.

Return to top


Property: Handle (read-write)
Inherited
See base class member description: System.Threading.WaitHandle.Handle

Summary
Gets or sets the native operating system handle.
C# Syntax:
public virtual IntPtr Handle {get; set;}
Exceptions
Exception Type Condition
SecurityException The caller does not have the required permission.
.NET Framework Security:
SecurityPermissionAttribute for access to unmanaged code. Associated enumeration: SecurityPermissionFlag.UnmanagedCode.

Return to top


Method: Close()
Inherited
See base class member description: System.Threading.WaitHandle.Close

Summary
When overridden in a derived class, releases all resources held by the current WaitHandle.
C# Syntax:
public virtual void Close();
Remarks
This method is the public version of the IDisposable.Dispose method implemented to support the IDisposable interface.

This method releases any unmanaged resources held by the current instance. This method can, but is not required to, suppress finalization during garbage collection by calling the GC.SuppressFinalize method.

Override this method to release resources allocated in derived classes.

Use this method to release all resources held by an instance of WaitHandle. Once this method is called, references to the current instance cause undefined behavior.

Return to top


Method: CreateObjRef(
   Type requestedType
)
Inherited
See base class member description: System.MarshalByRefObject.CreateObjRef

Summary
Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.
C# Syntax:
public virtual ObjRef CreateObjRef(
   Type requestedType
);
Parameters:

requestedType

The Type of the object that the new ObjRef will reference.

Return Value:
Information required to generate a proxy.
Exceptions
Exception Type Condition
RemotingException This instance is not a valid remoting object.

Return to top


Method: Dispose(
   bool explicitDisposing
)
Inherited
See base class member description: System.Threading.WaitHandle.Dispose

Summary
When overridden in a derived class, releases the unmanaged resources used by the WaitHandle, and optionally releases the managed resources.
C# Syntax:
protected virtual void Dispose(
   bool explicitDisposing
);
Parameters:

explicitDisposing

true to release both managed and unmanaged resources; false to release only unmanaged resources.

Remarks
This method is called by the WaitHandle.Dispose() method and the Finalize method.Dispose() invokes this protected method with the explicitDisposing parameter set to true.Finalize invokes Dispose with explicitDisposing set to false.

When the explicitDisposing parameter is true, this method releases all resources held by any managed objects that this WaitHandle references. This method invokes the Dispose() method of each referenced object.

Dispose can be called multiple times by other objects. When overriding this method, be careful not to reference objects that have been previously disposed in an earlier call to Dispose or Close.

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Threading.WaitHandle.Finalize

Summary
Releases the resources held by the current instance.
C# Syntax:
~WaitHandle();
Remarks
Application code does not call this method; it is automatically invoked during garbage collection, unless finalization by the garbage collector has been disabled. For more information, see GC.SuppressFinalize and Object.Finalize. This method overrides System.Object.Finalize.

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetLifetimeService()
Inherited
See base class member description: System.MarshalByRefObject.GetLifetimeService

Summary
Retrieves the current lifetime service object that controls the lifetime policy for this instance.
C# Syntax:
public object GetLifetimeService();
Return Value:
An object of type ILease used to control the lifetime policy for this instance.
Remarks
For more information about lifetime services, see the LifetimeServices class.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: InitializeLifetimeService()
Inherited
See base class member description: System.MarshalByRefObject.InitializeLifetimeService

Summary
Obtains a lifetime service object to control the lifetime policy for this instance.
C# Syntax:
public virtual object InitializeLifetimeService();
Return Value:
An object of type ILease used to control the lifetime policy for this instance. This is the current lifetime service object for this instance if one exists; otherwise, a new lifetime service object initialized to the value of the LifetimeServices.LeaseManagerPollTime property.
Remarks
For more information about lifetime services, see the LifetimeServices class.
Example
The following code example demonstrates creating a lease.
 public class MyClass : MarshalByRefObject
 {
   public override Object InitializeLifetimeService()
   {
     ILease lease = (ILease)base.InitializeLifetimeService();
     if (lease.CurrentState == LeaseState.Initial)
     {
          lease.InitialLeaseTime = TimeSpan.FromMinutes(1);
          lease.SponsorshipTimeout = TimeSpan.FromMinutes(2);
           lease.RenewOnCallTime = TimeSpan.FromSeconds(2);
     }
       return lease;
   }
 }

    

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: ReleaseMutex()
Summary
Releases the Mutex once.
C# Syntax:
public void ReleaseMutex();
Exceptions
Exception Type Condition
ApplicationException The calling thread does not own the mutex.
Remarks
A thread that owns a mutex can specify the same mutex in repeated wait function calls without blocking its execution. The number of calls is kept by the runtime. The thread must call Mutex.ReleaseMutex the same number of times to release ownership of the mutex. If a thread terminates normally while owning a mutex, the state of the mutex is set to signaled and the next waiting thread gets ownership. If no one owns the mutex, the state of the mutex is signaled.

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Overloaded Method: WaitOne()
Inherited
See base class member description: System.Threading.WaitHandle.WaitOne

Summary
When overridden in a derived class, blocks the current thread until the current WaitHandle receives a signal.
C# Syntax:
public virtual bool WaitOne();
Return Value:
true if the current instance receives a signal; otherwise, false.
Remarks
The caller of this method blocks indefinitely until the current instance receives a signal. Use this method to block until a WaitHandle receives a signal from another thread, such as is generated when an asynchronous operation completes. For more information, see the System.IAsyncResult interface.

Override this method to customize the behavior of derived classes.

Return to top


Overloaded Method: WaitOne(
   int millisecondsTimeout,
   bool exitContext
)
Inherited
See base class member description: System.Threading.WaitHandle.WaitOne

Summary
When overridden in a derived class, blocks the current thread until the current WaitHandle receives a signal, using 32-bit signed integer to measure the time interval and specifying whether to exit the synchronization domain before the wait.
C# Syntax:
public virtual bool WaitOne(
   int millisecondsTimeout,
   bool exitContext
);
Parameters:

millisecondsTimeout

The number of milliseconds to wait for the thread to receive a signal.

exitContext

true to exit the synchronization domain for the context before the wait (if in a synchronized context), and reacquire it; otherwise, false.

Return Value:
true if the current instance receives a signal; otherwise, false.
Remarks
The caller of this method blocks indefinitely until the current instance receives a signal. Use this method to block until a WaitHandle receives a signal from another thread, such as is generated when an asynchronous operation completes. For more information, see the System.IAsyncResult interface.

Override this method to customize the behavior of derived classes.

Return to top


Overloaded Method: WaitOne(
   TimeSpan timeout,
   bool exitContext
)
Inherited
See base class member description: System.Threading.WaitHandle.WaitOne

Summary
When overridden in a derived class, blocks the current thread until the current instance receives a signal, using a TimeSpan to measure the time interval and specifying whether to exit the synchronization domain before the wait.
C# Syntax:
public virtual bool WaitOne(
   TimeSpan timeout,
   bool exitContext
);
Parameters:

timeout

The number of milliseconds to wait for the thread to receive a signal.

exitContext

true to exit the synchronization domain for the context before the wait (if in a synchronized context), and reacquire it; otherwise, false.

Return Value:
true if the current instance receives a signal; otherwise, false.
Remarks
The caller of this method blocks indefinitely until the current instance receives a signal. Use this method to block until a WaitHandle receives a signal from another thread, such as is generated when an asynchronous operation completes. For more information, see the System.IAsyncResult interface.

Override this method to customize the behavior of derived classes.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.