This documentation is archived and is not being maintained.

Mutex Constructor

.NET Framework 1.1

Initializes a new instance of the Mutex class.

Overload List

Initializes a new instance of the Mutex class with default properties.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New()
[C#] public Mutex();
[C++] public: Mutex();
[JScript] public function Mutex();

Initializes a new instance of the Mutex class with a Boolean value indicating whether the calling thread should have initial ownership of the mutex.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(Boolean)
[C#] public Mutex(bool);
[C++] public: Mutex(bool);
[JScript] public function Mutex(Boolean);

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.

[Visual Basic] Public Sub New(Boolean, String)
[C#] public Mutex(bool, string);
[C++] public: Mutex(bool, String*);
[JScript] public function Mutex(Boolean, String);

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.

[Visual Basic] Public Sub New(Boolean, String, ByRef Boolean)
[C#] public Mutex(bool, string, bool);
[C++] public: Mutex(bool, String*, bool);
[JScript] public function Mutex(Boolean, String, Boolean);

Example

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of the Mutex constructor. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
' This example shows how a named mutex is used to signal between
' processes or threads.
' Run this program from two (or more) command windows. Each process
' creates a Mutex object that represents the named mutex "MyMutex".
' The named mutex is a system object whose lifetime is bounded by the
' lifetimes of the Mutex objects that represent it. The named mutex
' is created when the first process creates its local Mutex; in this
' example, the named mutex is owned by the first process. The named 
' mutex is destroyed when all the Mutex objects that represent it
' have been released. 
' The second process (and any subsequent process) waits for earlier
' processes to release the named mutex.

Imports System
Imports System.Threading

Public Class Test
   
   Public Shared Sub Main()
      ' Set this variable to false if you do not want to request 
      ' initial ownership of the named mutex.
      Dim requestInitialOwnership As Boolean = True
      Dim mutexWasCreated As Boolean
      
      ' Request initial ownership of the named mutex by passing
      ' true for the first parameter. Only one system object named 
      ' "MyMutex" can exist; the local Mutex object represents 
      ' this system object. If "MyMutex" is created by this call,
      ' then mutexWasCreated contains true; otherwise, it contains
      ' false.
      Dim m As New Mutex(requestInitialOwnership, "MyMutex", _
          mutexWasCreated)
      
      ' This thread owns the mutex only if it both requested 
      ' initial ownership and created the named mutex. Otherwise,
      ' it can request the named mutex by calling WaitOne.
      If Not (requestInitialOwnership And mutexWasCreated) Then
         Console.WriteLine("Waiting for the named mutex.")
         m.WaitOne()
      End If
      
      ' Once the process has gained control of the named mutex,
      ' hold onto it until the user presses ENTER.
      Console.WriteLine("This process owns the named mutex. " _
          & "Press ENTER to release the mutex and exit.")
      Console.ReadLine()
      
      ' Call ReleaseMutex to allow other threads to gain control
      ' of the named mutex. If you keep a reference to the local
      ' Mutex, you can call WaitOne to request control of the 
      ' named mutex.
      m.ReleaseMutex()
   End Sub 'Main
End Class 'Test

[C#] 
// This example shows how a named mutex is used to signal between
// processes or threads.
// Run this program from two (or more) command windows. Each process
// creates a Mutex object that represents the named mutex "MyMutex".
// The named mutex is a system object whose lifetime is bounded by the
// lifetimes of the Mutex objects that represent it. The named mutex
// is created when the first process creates its local Mutex; in this
// example, the named mutex is owned by the first process. The named 
// mutex is destroyed when all the Mutex objects that represent it
// have been released. 
// The second process (and any subsequent process) waits for earlier
// processes to release the named mutex.

using System;
using System.Threading;

public class Test
{
    public static void Main()
    {
        // Set this variable to false if you do not want to request 
        // initial ownership of the named mutex.
        bool requestInitialOwnership = true;
        bool mutexWasCreated;

        // Request initial ownership of the named mutex by passing
        // true for the first parameter. Only one system object named 
        // "MyMutex" can exist; the local Mutex object represents 
        // this system object. If "MyMutex" is created by this call,
        // then mutexWasCreated contains true; otherwise, it contains
        // false.
        Mutex m = new Mutex(requestInitialOwnership, 
                            "MyMutex", 
                            out mutexWasCreated);
        
        // This thread owns the mutex only if it both requested 
        // initial ownership and created the named mutex. Otherwise,
        // it can request the named mutex by calling WaitOne.
        if (!(requestInitialOwnership && mutexWasCreated))
        {
            Console.WriteLine("Waiting for the named mutex.");
            m.WaitOne();
        }

        // Once the process has gained control of the named mutex,
        // hold onto it until the user presses ENTER.
        Console.WriteLine("This process owns the named mutex. " +
            "Press ENTER to release the mutex and exit.");
        Console.ReadLine();

        // Call ReleaseMutex to allow other threads to gain control
        // of the named mutex. If you keep a reference to the local
        // Mutex, you can call WaitOne to request control of the 
        // named mutex.
        m.ReleaseMutex();
    }
}

[C++] 
// This example shows how a named mutex is used to signal between
// processes or threads.
// Run this program from two (or more) command windows. Each process
// creates a Mutex object that represents the named mutex "MyMutex".
// The named mutex is a system object whose lifetime is bounded by the
// lifetimes of the Mutex objects that represent it. The named mutex
// is created when the first process creates its local Mutex; in this
// example, the named mutex is owned by the first process. The named 
// mutex is destroyed when all the Mutex objects that represent it
// have been released. 
// The second process (and any subsequent process) waits for earlier
// processes to release the named mutex.

#using <mscorlib.dll>

using namespace System;
using namespace System::Threading;

int main()
{
    // Set this variable to false if you do not want to request 
    // initial ownership of the named mutex.
    bool requestInitialOwnership = true;
    bool mutexWasCreated;

    // Request initial ownership of the named mutex by passing
    // true for the first parameter. Only one system object named 
    // "MyMutex" can exist; the local Mutex object represents 
    // this system object. If "MyMutex" is created by this call,
    // then mutexWasCreated contains true; otherwise, it contains
    // false.
    Mutex* m = new Mutex(requestInitialOwnership, 
                        "MyMutex", 
                        &mutexWasCreated);
        
    // This thread owns the mutex only if it both requested 
    // initial ownership and created the named mutex. Otherwise,
    // it can request the named mutex by calling WaitOne.
    if (!(requestInitialOwnership && mutexWasCreated))
    {
        Console::WriteLine("Waiting for the named mutex.");
        m->WaitOne();
    }

    // Once the process has gained control of the named mutex,
    // hold onto it until the user presses ENTER.
    Console::WriteLine("This process owns the named mutex. "
        "Press ENTER to release the mutex and exit.");
    Console::ReadLine();

    // Call ReleaseMutex to allow other threads to gain control
    // of the named mutex. If you keep a reference to the local
    // Mutex, you can call WaitOne to request control of the 
    // named mutex.
    m->ReleaseMutex();
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

Mutex Class | Mutex Members | System.Threading Namespace

Show: