Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Mutex.Mutex(Boolean, String, Boolean) Constructor

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.

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

public Mutex (
	bool initiallyOwned,
	string name,
	out bool createdNew
)
public Mutex (
	boolean initiallyOwned, 
	String name, 
	/** @attribute OutAttribute() */ /** @ref */ boolean createdNew
)
public function Mutex (
	initiallyOwned : boolean, 
	name : String, 
	createdNew : boolean
)
Not applicable.

Parameters

initiallyOwned

true to give the calling thread initial ownership of the named system mutex if the named system mutex is created as a result of this call; otherwise, false.

name

The name of the Mutex. If the value is a null reference (Nothing in Visual Basic), the Mutex is unnamed.

createdNew

When this method returns, contains a Boolean that is true if a local mutex was created (that is, if name is a null reference (Nothing in Visual Basic) or an empty string) or if the specified named system mutex was created; false if the specified named system mutex already existed. This parameter is passed uninitialized.

Exception typeCondition

UnauthorizedAccessException

The named mutex exists and has access control security, but the user does not have System.Security.AccessControl.MutexRights.FullControl.

IOException

A Win32 error occurred.

ApplicationException

The named mutex cannot be created, perhaps because a wait handle of a different type has the same name.

ArgumentException

name is longer than 260 characters.

If name is not a null reference (Nothing in Visual Basic) and initiallyOwned is true, the calling thread owns the named mutex only if createdNew is true after the call. Otherwise the thread can request the mutex by calling the WaitOne method.

This constructor initializes a Mutex object that represents a named system mutex. You can create multiple Mutex objects that represent the same named system mutex.

If the named mutex has already been created with access control security, and the caller does not have System.Security.AccessControl.MutexRights.FullControl rights, an exception is thrown. To open an existing named mutex with only those permissions needed for synchronizing thread activities, see the OpenExisting method.

If you specify a null reference (Nothing in Visual Basic) or an empty string for name, a local mutex is created, as if you had called the Mutex(Boolean) constructor. In this case, createdNew is always true.

Because they are system-wide, named mutexes can be used to coordinate resource use across process boundaries.

NoteNote:

On a server that is running Terminal Services, a named system mutex can have two levels of visibility. If its name begins with the prefix "Global\", the mutex is visible in all terminal server sessions. If its name begins with the prefix "Local\", the mutex is visible only in the terminal server session where it was created. In that case, a separate mutex with the same name can exist in each of the other terminal server sessions on the server. If you do not specify a prefix when you create a named mutex, it takes the prefix "Local\". 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. That is, the prefix names "Global\" and "Local\" describe the scope of the mutex name relative to terminal server sessions, not relative to processes.

The following code 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. In this example, its 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 object, and destroyed when all the Mutex objects that represent it have been released. The named mutex is initially owned by the first process. The second process and any subsequent processes wait for earlier processes to release the named mutex.

// 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();
    }
}

// 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.
import System.*;
import System.Threading.*;
import System.Threading.Thread;

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

        // 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();
    } //main
} //Test

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2015 Microsoft