Mutex.TryOpenExisting 方法

定義

開啟指定的具名 mutex (如果已經存在),並傳回值,指出作業是否成功。Opens a specified named mutex, if it already exists, and returns a value that indicates whether the operation succeeded.

多載

TryOpenExisting(String, Mutex)

開啟指定的具名 mutex (如果已經存在),並傳回值,指出作業是否成功。Opens the specified named mutex, if it already exists, and returns a value that indicates whether the operation succeeded.

TryOpenExisting(String, MutexRights, Mutex)

使用所需的安全性存取權,開啟指定的具名 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)

開啟指定的具名 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 的 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

如果已成功開啟具名 Mutex,則為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.

具名的 Mutex 已存在,但使用者沒有使用它所需的安全性存取權。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,請使用其中一個具有參數的函式 Mutex nameTo create the system mutex when it does not already exist, use one of the Mutex constructors that has a name parameter.

如果您不確定命名 mutex 是否存在,請使用這個方法多載,而不是方法多載 OpenExisting(String) ,如果 mutex 不存在,則會擲回例外狀況。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 使用位 or 運算結合指定和許可權。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 旗標可讓執行緒在 mutex 上等待,並指定旗標 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)

使用所需的安全性存取權,開啟指定的具名 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 的 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

如果已成功開啟具名 Mutex,則為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.

具名的 Mutex 已存在,但使用者沒有使用它所需的安全性存取權。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,請使用其中一個具有參數的函式 Mutex nameTo create the system mutex when it does not already exist, use one of the Mutex constructors that has a name parameter.

如果您不確定命名 mutex 是否存在,請使用這個方法多載,而不是方法多載 OpenExisting(String, MutexRights) ,如果 mutex 不存在,則會擲回例外狀況。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 允許執行緒在 mutex 上等候的旗標,以及 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.

適用於