Mutex.TryOpenExisting 方法

定义

打开指定的已命名的互斥体(如果已经存在),并返回指示操作是否成功的值。Opens a specified named mutex, if it already exists, 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.

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);
public static bool TryOpenExisting (string name, out System.Threading.Mutex? result);
public static bool TryOpenExisting (string name, out System.Threading.Mutex result);
[<System.Security.SecurityCritical>]
static member TryOpenExisting : string * Mutex -> bool
static member TryOpenExisting : string * Mutex -> bool
Public Shared Function TryOpenExisting (name As String, ByRef result As Mutex) As Boolean

参数

name
String

要与其他进程共享的同步对象的名称。The name of the synchronization object to be shared with other processes. 该名称区分大小写。The name is case-sensitive.

result
Mutex

当此方法返回时,如果调用成功,则包含表示命名互斥体的 Mutex 对象;否则为 nullWhen 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.

返回

Boolean

如果命名互斥体成功打开,则为 true;否则为 falsetrue if the named mutex was opened successfully; otherwise, false. 在某些情况下,对于无效名称,可能会返回 falseIn some cases, false may be returned for invalid names.

属性

例外

name 是一个空字符串。name is an empty string.

-or-

仅限 .NET Framework:name 的长度超过 MAX_PATH(260 个字符)。.NET Framework only: name is longer than MAX_PATH (260 characters).

name 上声明的默认值为 nullname is null.

name 无效。name is invalid. 导致这种情况的原因有很多,包括操作系统可能会施加的一些限制,例如未知前缀或无效字符。This can be for various reasons, including some restrictions that may be placed by the operating system, such as an unknown prefix or invalid characters. 请注意,名称和常见前缀 "Global" 和 "Local" 区分大小写。Note that the name and common prefixes "Global" and "Local" are case-sensitive. 对于某些无效名称,该方法可能返回 falseFor some invalid names, the method may return false instead.

-or-

存在其他错误。There was some other error. HResult 属性可能提供更多信息。The HResult property may provide more information.

name 太长。The name is too long. 长度限制可能取决于操作系统或配置。Length restrictions may depend on the operating system or configuration.

存在命名的互斥,但用户没有使用它时所需的安全访问权限。The named mutex exists, but the user does not have the security access required to use it.

注解

name可以使用 Global\ 或前缀 Local\ 来指定命名空间。The name may be prefixed with Global\ or Local\ to specify a namespace. 如果 Global 指定了命名空间,则同步对象可能与系统上的任何进程共享。When the Global namespace is specified, the synchronization object may be shared with any processes on the system. Local指定命名空间后,如果未指定命名空间,这也是默认命名空间,则同步对象可能与同一会话中的进程共享。When the Local namespace is specified, which is also the default when no namespace is specified, the synchronization object may be shared with processes in the same session. 在 Windows 上,会话是一个登录会话,服务通常在不同的非交互式会话中运行。On Windows, a session is a login session, and services typically run in a different non-interactive session. 在类似 Unix 的操作系统上,每个 shell 都有自己的会话。On Unix-like operating systems, each shell has its own session. 会话本地同步对象可能适用于具有父/子关系的进程之间的同步,它们全都在同一会话中运行。Session-local synchronization objects may be appropriate for synchronizing between processes with a parent/child relationship where they all run in the same session. 有关 Windows 上的 synchornization 对象名称的详细信息,请参阅 对象名称For more information about synchornization object names on Windows, see Object Names.

如果命名空间中存在请求类型的同步对象,则会打开现有的同步对象。If a synchronization object of the requested type exists in the namespace, the existing synchronization object is opened. 如果命名空间中不存在同步对象,或者命名空间中存在其他类型的同步对象, false 则返回。If a synchronization object does not exist in the namespace, or a synchronization object of a different type exists in the namespace, false is returned.

若要创建不存在的系统互斥体,请使用 Mutex 具有参数的构造函数之一 nameTo create the system mutex when it does not already exist, use one of the Mutex constructors that has a name parameter.

如果不确定是否存在已命名的互斥体,请使用此方法重载,而不是 OpenExisting(String) 方法重载,这会在互斥体不存在时引发异常。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.

对此方法使用相同值的多个调用 name 不一定会返回相同的 Mutex 对象,即使返回的对象表示相同的命名系统 mutex。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.

此方法重载等效于调用 TryOpenExisting(String, MutexRights, Mutex) 方法重载,并 MutexRights.Synchronize MutexRights.Modify 使用按位 "或" 运算来指定和权限。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. 指定标志后, MutexRights.Synchronize 线程可以等待互斥体,指定 MutexRights.Modify 标志允许线程调用 ReleaseMutex 方法。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.

此方法不请求 mutex 的所有权。This method does not request ownership of the mutex.

适用于

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);
public static bool TryOpenExisting (string name, System.Security.AccessControl.MutexRights rights, out System.Threading.Mutex result);
[<System.Security.SecurityCritical>]
static member TryOpenExisting : string * System.Security.AccessControl.MutexRights * Mutex -> bool
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

参数

name
String

要与其他进程共享的同步对象的名称。The name of the synchronization object to be shared with other processes. 该名称区分大小写。The name is case-sensitive.

rights
MutexRights

表示所需的安全访问权限的枚举值的按位组合。A bitwise combination of the enumeration values that represent the desired security access.

result
Mutex

当此方法返回时,如果调用成功,则包含表示命名互斥体的 Mutex 对象;否则为 nullWhen 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.

返回

Boolean

如果命名互斥体成功打开,则为 true;否则为 falsetrue if the named mutex was opened successfully; otherwise, false. 在某些情况下,对于无效名称,可能会返回 falseIn some cases, false may be returned for invalid names.

属性

例外

name 是一个空字符串。name is an empty string.

-or-

仅限 .NET Framework:name 的长度超过 MAX_PATH(260 个字符)。.NET Framework only: name is longer than MAX_PATH (260 characters).

name 上声明的默认值为 nullname is null.

name 无效。name is invalid. 导致这种情况的原因有很多,包括操作系统可能会施加的一些限制,例如未知前缀或无效字符。This can be for various reasons, including some restrictions that may be placed by the operating system, such as an unknown prefix or invalid characters. 请注意,名称和常见前缀 "Global" 和 "Local" 区分大小写。Note that the name and common prefixes "Global" and "Local" are case-sensitive. 对于某些无效名称,该方法可能返回 falseFor some invalid names, the method may return false instead.

-or-

存在其他错误。There was some other error. HResult 属性可能提供更多信息。The HResult property may provide more information.

name 太长。The name is too long. 长度限制可能取决于操作系统或配置。Length restrictions may depend on the operating system or configuration.

存在命名的互斥,但用户没有使用它时所需的安全访问权限。The named mutex exists, but the user does not have the security access required to use it.

注解

name可以使用 Global\ 或前缀 Local\ 来指定命名空间。The name may be prefixed with Global\ or Local\ to specify a namespace. 如果 Global 指定了命名空间,则同步对象可能与系统上的任何进程共享。When the Global namespace is specified, the synchronization object may be shared with any processes on the system. Local指定命名空间后,如果未指定命名空间,这也是默认命名空间,则同步对象可能与同一会话中的进程共享。When the Local namespace is specified, which is also the default when no namespace is specified, the synchronization object may be shared with processes in the same session. 在 Windows 上,会话是一个登录会话,服务通常在不同的非交互式会话中运行。On Windows, a session is a login session, and services typically run in a different non-interactive session. 在类似 Unix 的操作系统上,每个 shell 都有自己的会话。On Unix-like operating systems, each shell has its own session. 会话本地同步对象可能适用于具有父/子关系的进程之间的同步,它们全都在同一会话中运行。Session-local synchronization objects may be appropriate for synchronizing between processes with a parent/child relationship where they all run in the same session. 有关 Windows 上的 synchornization 对象名称的详细信息,请参阅 对象名称For more information about synchornization object names on Windows, see Object Names.

如果命名空间中存在请求类型的同步对象,则会打开现有的同步对象。If a synchronization object of the requested type exists in the namespace, the existing synchronization object is opened. 如果命名空间中不存在同步对象,或者命名空间中存在其他类型的同步对象, false 则返回。If a synchronization object does not exist in the namespace, or a synchronization object of a different type exists in the namespace, false is returned.

若要创建不存在的系统互斥体,请使用 Mutex 具有参数的构造函数之一 nameTo create the system mutex when it does not already exist, use one of the Mutex constructors that has a name parameter.

如果不确定是否存在已命名的互斥体,请使用此方法重载,而不是 OpenExisting(String, MutexRights) 方法重载,这会在互斥体不存在时引发异常。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.

rights参数必须包括 MutexRights.Synchronize 允许线程在互斥体上等待的标志,以及 MutexRights.Modify 允许线程调用方法的标志 ReleaseMutexThe 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.

对此方法使用相同值的多个调用 name 不一定会返回相同的 Mutex 对象,即使返回的对象表示相同的命名系统 mutex。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.

此方法不请求 mutex 的所有权。This method does not request ownership of the mutex.

适用于