Export (0) Print
Expand All

Mutex Constructor (Boolean, String)

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.

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

public Mutex (
	bool initiallyOwned,
	string name
)
public Mutex (
	boolean initiallyOwned, 
	String name
)
public function Mutex (
	initiallyOwned : boolean, 
	name : String
)

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.

Exception typeCondition

UnauthorizedAccessException

The named mutex exists and has access control security, but the user does not have 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 mutex only if the named system mutex was created as a result of this call. Since there is no mechanism for determining whether the named system mutex was created, it is better to specify false for initiallyOwned when calling this constructor overload. You can use the Mutex(Boolean,String,Boolean) constructor if you need to determine initial ownership.

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 MutexRights.FullControl, 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.

// 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 constructor overload shown here cannot tell the calling thread
// whether initial ownership of the named mutex was granted. Therefore,
// do not request initial ownership unless you are certain that the 
// thread will create the named mutex.


using System;
using System.Threading;

public class Test
{
    public static void Main()
    {
        // Create the named mutex. Only one system object named 
        // "MyMutex" can exist; the local Mutex object represents 
        // this system object, regardless of which process or thread
        // caused "MyMutex" to be created.
        Mutex m = new Mutex(false, "MyMutex");
        
        // Try to gain control of the named mutex. If the mutex is 
        // controlled by another thread, wait for it to be released.        
        Console.WriteLine("Waiting for the Mutex.");
        m.WaitOne();

        // Keep control of the mutex until the user presses
        // ENTER.
        Console.WriteLine("This application owns the mutex. " +
            "Press ENTER to release the mutex and exit.");
        Console.ReadLine();

        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 constructor overload shown here cannot tell the calling thread
// whether initial ownership of the named mutex was granted. Therefore,
// do not request initial ownership unless you are certain that the 
// thread will create the named mutex.

import System.*;
import System.Threading.*;
import System.Threading.Thread;

public class Test
{
    public static void main(String[] args)
    {
        // Create the named mutex. Only one system object named 
        // "MyMutex" can exist; the local Mutex object represents 
        // this system object, regardless of which process or thread
        // caused "MyMutex" to be created.
        Mutex m = new Mutex(false, "MyMutex");

        // Try to gain control of the named mutex. If the mutex is 
        // controlled by another thread, wait for it to be released.        
        Console.WriteLine("Waiting for the Mutex.");
        m.WaitOne();

        // Keep control of the mutex until the user presses
        // ENTER.
        Console.WriteLine(("This application owns the mutex. " 
            +  "Press ENTER to release the mutex and exit."));
        Console.ReadLine();
        m.ReleaseMutex();
    } //main
} //Test

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft