Mutex.TryOpenExisting Method

Definition

Opens a specified named mutex, if it already exists, and returns a value that indicates whether the operation succeeded.

Overloads

TryOpenExisting(String, Mutex)

Opens the specified named mutex, if it already exists, and returns a value that indicates whether the operation succeeded.

TryOpenExisting(String, MutexRights, Mutex)

Opens the specified named mutex, if it already exists, with the desired security access, and returns a value that indicates whether the operation succeeded.

TryOpenExisting(String, Mutex)

Opens the specified named mutex, if it already exists, and returns a value that indicates whether the operation succeeded.

public:
 static bool TryOpenExisting(System::String ^ name, [Runtime::InteropServices::Out] System::Threading::Mutex ^ % result);
[System.Security.SecurityCritical]
public static bool TryOpenExisting (string name, out System.Threading.Mutex result);
static member TryOpenExisting : string * Mutex -> bool
Public Shared Function TryOpenExisting (name As String, ByRef result As Mutex) As Boolean

Parameters

name
String

The name of the system mutex to open.

result
Mutex

When this method returns, contains a Mutex object that represents the named mutex if the call succeeded, or null if the call failed. This parameter is treated as uninitialized.

Returns

true if the named mutex was opened successfully; otherwise, false.

Attributes

Exceptions

name is an empty string.

-or-

name is longer than 260 characters.

name is null.

A Win32 error occurred.

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

Remarks

If the named mutex does not exist, this method does not create it. To create the system mutex when it does not already exist, use one of the Mutex constructors that has a name parameter.

If you are uncertain whether a named mutex exists, use this method overload instead of the OpenExisting(String) method overload, which throws an exception if the mutex does not exist.

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 TryOpenExisting(String, MutexRights, Mutex) 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.

Security

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

TryOpenExisting(String, MutexRights, Mutex)

Opens the specified named mutex, if it already exists, with the desired security access, and returns a value that indicates whether the operation succeeded.

public:
 static bool TryOpenExisting(System::String ^ name, System::Security::AccessControl::MutexRights rights, [Runtime::InteropServices::Out] System::Threading::Mutex ^ % result);
[System.Security.SecurityCritical]
public static bool TryOpenExisting (string name, System.Security.AccessControl.MutexRights rights, out System.Threading.Mutex result);
static member TryOpenExisting : string * System.Security.AccessControl.MutexRights * Mutex -> bool
Public Shared Function TryOpenExisting (name As String, rights As MutexRights, ByRef result As Mutex) As Boolean

Parameters

name
String

The name of the system mutex to open.

rights
MutexRights

A bitwise combination of the enumeration values that represent the desired security access.

result
Mutex

When this method returns, contains a Mutex object that represents the named mutex if the call succeeded, or null if the call failed. This parameter is treated as uninitialized.

Returns

true if the named mutex was opened successfully; otherwise, false.

Attributes

Exceptions

name is an empty string.

-or-

name is longer than 260 characters.

name is null.

A Win32 error occurred.

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

Remarks

If the named mutex does not exist, this method does not create it. To create the system mutex when it does not already exist, use one of the Mutex constructors that has a name parameter.

If you are uncertain whether a named mutex exists, use this method overload instead of the OpenExisting(String, MutexRights) method overload, which throws an exception if the mutex does not exist.

The rights parameter must include the MutexRights.Synchronize flag to allow threads to wait on the mutex, and the MutexRights.Modify flag to allow threads to call the ReleaseMutex method.

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 does not request ownership of the mutex.

Security

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

Applies to