Export (0) Print
Expand All

Mutex.OpenExisting Method (String)

Opens the specified named mutex, if it already exists.

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

static member OpenExisting : 
        name:string -> Mutex

Parameters

name
Type: System.String

The name of the system mutex to open.

Return Value

Type: System.Threading.Mutex
An object that represents the named system mutex.

ExceptionCondition
ArgumentException

name is an empty string.

-or-

name is longer than 260 characters.

ArgumentNullException

name is a null reference (Nothing in Visual Basic).

WaitHandleCannotBeOpenedException

The named mutex does not exist.

IOException

A Win32 error occurred.

UnauthorizedAccessException

The named mutex exists, but the user does not have the security access required to use it.

The OpenExisting method tries to open the specified named system mutex. If the system mutex does not exist, this method throws an exception instead of creating the system object. To create the system mutex when it does not already exist, use one of the Mutex constructors that has a name parameter.

Multiple calls to this method that use the same value for name do not necessarily return the same Mutex object, even though the objects that are returned represent the same named system mutex.

This method overload is equivalent to calling the OpenExisting(String, MutexRights) method overload and specifying MutexRights.Synchronize and MutexRights.Modify rights, combined by using the bitwise OR operation.

Specifying the MutexRights.Synchronize flag allows a thread to wait on the mutex, and specifying the MutexRights.Modify flag allows a thread to call the ReleaseMutex method.

This method does not request ownership of the mutex.

The following code example demonstrates the cross-process behavior of a named mutex with access control security. The example uses the OpenExisting(String) method overload to test for the existence of a named mutex.

If the mutex does not exist, it is created with initial ownership and access control security that denies the current user the right to use the mutex, but grants the right to read and change permissions on the mutex.

If you run the compiled example from two command windows, the second copy will throw an access violation exception on the call to OpenExisting(String). The exception is caught, and the example uses the OpenExisting(String, MutexRights) method overload to open the mutex with the rights needed to read and change the permissions.

After the permissions are changed, the mutex is opened with the rights required to enter and release it. If you run the compiled example from a third command window, it runs using the new permissions.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

  • SecurityCriticalAttribute 

    Requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft