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.
Assembly: mscorlib (in mscorlib.dll)
<SecurityCriticalAttribute> Public Sub New ( initiallyOwned As Boolean, name As String )
The named mutex exists and has access control security, but the user does not have MutexRights.FullControl.
A Win32 error occurred.
The named mutex cannot be created, perhaps because a wait handle of a different type has the same name.
name is longer than 260 characters.
If name is not null 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.
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 null 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.
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 example shows how a named mutex is used to signal between threads running in two separate processes.
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 Mutex object; in this example, the named mutex is owned by the first process that runs the program. The named mutex is destroyed when all the Mutex objects that represent it have been released.
The constructor overload used in this example cannot tell the calling thread whether initial ownership of the named mutex was granted. You should not use this constructor to request initial ownership unless you can be certain that the thread will create 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 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. Imports System Imports System.Threading Public Class Test <MTAThread> _ Public Shared Sub 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. Dim m As 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() End Sub 'Main End Class 'Test
Requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.
Available since 4.5
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 7.1
Available since 8.1