共用方式為


Mutex 建構函式

定義

初始化 Mutex 類別的新執行個體。

多載

Mutex()

使用預設屬性,初始化 Mutex 類別的新執行個體。

Mutex(Boolean)

使用布林值 (Boolean),初始化 Mutex 類別的新執行個體,指出呼叫執行緒是否應該具有 Mutex 的初始擁有權。

Mutex(Boolean, String)

使用布林值,初始化 Mutex 類別的新執行個體,指出呼叫執行緒是否應該具有 Mutex 的初始擁有權,並擁有為 Mutex 名稱的字串。

Mutex(Boolean, String, Boolean)

使用表示呼叫執行緒是否應該具有 Mutex 的初始擁有權的布林值、代表 Mutex 名稱的字串,以及當方法傳回時表示是否將 Mutex 的初始擁有權授與呼叫執行緒的布林值,初始化 Mutex 類別的新執行個體。

Mutex(Boolean, String, Boolean, MutexSecurity)

使用表示呼叫執行緒是否應該具有 Mutex 的初始擁有權的布林值、代表 Mutex 名稱的字串、當這個方法傳回時表示是否將 Mutex 的初始擁有權授與呼叫執行緒的布林值,以及要套用至具名 Mutex 的存取控制安全性,初始化 Mutex 類別的新執行個體。

Mutex()

來源:
Mutex.cs
來源:
Mutex.cs
來源:
Mutex.cs

使用預設屬性,初始化 Mutex 類別的新執行個體。

public:
 Mutex();
public Mutex ();
Public Sub New ()

範例

下列程式代碼範例示範如何使用本機 Mutex 物件來同步處理受保護資源的存取。 建立 Mutex 的線程一開始不會擁有它。

// This example shows how a Mutex is used to synchronize access
// to a protected resource. Unlike Monitor, Mutex can be used with
// WaitHandle.WaitAll and WaitAny, and can be passed across
// AppDomain boundaries.
using namespace System;
using namespace System::Threading;
const int numIterations = 1;
const int numThreads = 3;
ref class Test
{
public:

   // Create a new Mutex. The creating thread does not own the
   // Mutex.
   static Mutex^ mut = gcnew Mutex;
   static void MyThreadProc()
   {
      for ( int i = 0; i < numIterations; i++ )
      {
         UseResource();

      }
   }


private:

   // This method represents a resource that must be synchronized
   // so that only one thread at a time can enter.
   static void UseResource()
   {
      
      //Wait until it is OK to enter.
      mut->WaitOne();
      Console::WriteLine( "{0} has entered protected the area", Thread::CurrentThread->Name );
      
      // Place code to access non-reentrant resources here.
      // Simulate some work.
      Thread::Sleep( 500 );
      Console::WriteLine( "{0} is leaving protected the area\r\n", Thread::CurrentThread->Name );
      
      // Release the Mutex.
      mut->ReleaseMutex();
   }

};

int main()
{
   
   // Create the threads that will use the protected resource.
   for ( int i = 0; i < numThreads; i++ )
   {
      Thread^ myThread = gcnew Thread( gcnew ThreadStart( Test::MyThreadProc ) );
      myThread->Name = String::Format( "Thread {0}", i + 1 );
      myThread->Start();

   }
   
   // The main thread exits, but the application continues to 
   // run until all foreground threads have exited.
}
// This example shows how a Mutex is used to synchronize access
// to a protected resource. Unlike Monitor, Mutex can be used with
// WaitHandle.WaitAll and WaitAny, and can be passed across
// AppDomain boundaries.
 
using System;
using System.Threading;

class Test13
{
    // Create a new Mutex. The creating thread does not own the
    // Mutex.
    private static Mutex mut = new Mutex();
    private const int numIterations = 1;
    private const int numThreads = 3;

    static void Main()
    {
        // Create the threads that will use the protected resource.
        for(int i = 0; i < numThreads; i++)
        {
            Thread myThread = new Thread(new ThreadStart(MyThreadProc));
            myThread.Name = String.Format("Thread{0}", i + 1);
            myThread.Start();
        }

        // The main thread exits, but the application continues to
        // run until all foreground threads have exited.
    }

    private static void MyThreadProc()
    {
        for(int i = 0; i < numIterations; i++)
        {
            UseResource();
        }
    }

    // This method represents a resource that must be synchronized
    // so that only one thread at a time can enter.
    private static void UseResource()
    {
        // Wait until it is safe to enter.
        mut.WaitOne();

        Console.WriteLine("{0} has entered the protected area", 
            Thread.CurrentThread.Name);

        // Place code to access non-reentrant resources here.

        // Simulate some work.
        Thread.Sleep(500);

        Console.WriteLine("{0} is leaving the protected area\r\n", 
            Thread.CurrentThread.Name);
         
        // Release the Mutex.
        mut.ReleaseMutex();
    }
}
' This example shows how a Mutex is used to synchronize access
' to a protected resource. Unlike Monitor, Mutex can be used with
' WaitHandle.WaitAll and WaitAny, and can be passed across
' AppDomain boundaries.
 
Imports System.Threading

Class Test
    ' Create a new Mutex. The creating thread does not own the
    ' Mutex.
    Private Shared mut As New Mutex()
    Private Const numIterations As Integer = 1
    Private Const numThreads As Integer = 3

    <MTAThread> _
    Shared Sub Main()
        ' Create the threads that will use the protected resource.
        Dim i As Integer
        For i = 1 To numThreads
            Dim myThread As New Thread(AddressOf MyThreadProc)
            myThread.Name = [String].Format("Thread{0}", i)
            myThread.Start()
        Next i

        ' The main thread exits, but the application continues to
        ' run until all foreground threads have exited.

    End Sub

    Private Shared Sub MyThreadProc()
        Dim i As Integer
        For i = 1 To numIterations
            UseResource()
        Next i
    End Sub

    ' This method represents a resource that must be synchronized
    ' so that only one thread at a time can enter.
    Private Shared Sub UseResource()
        ' Wait until it is safe to enter.
        mut.WaitOne()

        Console.WriteLine("{0} has entered protected area", _
            Thread.CurrentThread.Name)

        ' Place code to access non-reentrant resources here.

        ' Simulate some work
        Thread.Sleep(500)

        Console.WriteLine("{0} is leaving protected area" & vbCrLf, _
            Thread.CurrentThread.Name)

        ' Release Mutex.
        mut.ReleaseMutex()
    End Sub
End Class

備註

呼叫這個建構函式多載與呼叫建 Mutex(Boolean) 構函式多載相同,並指定 false mutex 的初始擁有權。 也就是說,呼叫線程不會擁有 mutex。

另請參閱

適用於

Mutex(Boolean)

來源:
Mutex.cs
來源:
Mutex.cs
來源:
Mutex.cs

使用布林值 (Boolean),初始化 Mutex 類別的新執行個體,指出呼叫執行緒是否應該具有 Mutex 的初始擁有權。

public:
 Mutex(bool initiallyOwned);
public Mutex (bool initiallyOwned);
new System.Threading.Mutex : bool -> System.Threading.Mutex
Public Sub New (initiallyOwned As Boolean)

參數

initiallyOwned
Boolean

如果要把 Mutex 的初始擁有權提供給呼叫執行緒,則為 true,否則為 false

範例

下列程式代碼範例示範如何使用本機 Mutex 物件來同步處理受保護資源的存取。 一開始建立 Mutex 它的線程。

using namespace System;
using namespace System::Threading;

const int numIterations = 1;
const int numThreads = 3;

ref class Test
{
public:

   // Create a new Mutex. The creating thread owns the
   // Mutex.
   static Mutex^ mut = gcnew Mutex( true );
   static void MyThreadProc()
   {
      for ( int i = 0; i < numIterations; i++ )
      {
         UseResource();

      }
   }


private:

   // This method represents a resource that must be synchronized
   // so that only one thread at a time can enter.
   static void UseResource()
   {
      
      //Wait until it is OK to enter.
      mut->WaitOne();
      Console::WriteLine( "{0} has entered protected the area", Thread::CurrentThread->Name );
      
      // Place code to access non-reentrant resources here.
      // Simulate some work.
      Thread::Sleep( 500 );
      Console::WriteLine( "{0} is leaving protected the area\r\n", Thread::CurrentThread->Name );
      
      // Release the Mutex.
      mut->ReleaseMutex();
   }

};

int main()
{
   
   // Initialize the Mutex.
   Mutex^ mut = Test::mut;
   
   // Create the threads that will use the protected resource.
   for ( int i = 0; i < numThreads; i++ )
   {
      Thread^ myThread = gcnew Thread( gcnew ThreadStart( Test::MyThreadProc ) );
      myThread->Name = String::Format( "Thread {0}", i + 1 );
      myThread->Start();

   }
   
   // Wait one second before allowing other threads to
   // acquire the Mutex.
   Console::WriteLine( "Creating thread owns the Mutex." );
   Thread::Sleep( 1000 );
   Console::WriteLine( "Creating thread releases the Mutex.\r\n" );
   mut->ReleaseMutex();
}
// The example displays output like the following:
//       Creating thread owns the Mutex.
//       Creating thread releases the Mutex.
//       
//       Thread1 has entered the protected area
//       Thread1 is leaving the protected area
//       
//       Thread2 has entered the protected area
//       Thread2 is leaving the protected area
//       
//       Thread3 has entered the protected area
//       Thread3 is leaving the protected area
using System;
using System.Threading;

class Test
{
    private static Mutex mut;
    private const int numIterations = 1;
    private const int numThreads = 3;

    static void Main()
    {
        // Create a new Mutex. The creating thread owns the Mutex.
        mut = new Mutex(true);
        // Create the threads that will use the protected resource.
        for(int i = 0; i < numThreads; i++)
        {
            Thread myThread = new Thread(new ThreadStart(MyThreadProc));
            myThread.Name = String.Format("Thread{0}", i + 1);
            myThread.Start();
        }

        // Wait one second before allowing other threads to
        // acquire the Mutex.
        Console.WriteLine("Creating thread owns the Mutex.");
        Thread.Sleep(1000);

        Console.WriteLine("Creating thread releases the Mutex.\r\n");
        mut.ReleaseMutex();
    }

    private static void MyThreadProc()
    {
        for(int i = 0; i < numIterations; i++)
        {
            UseResource();
        }
    }

    // This method represents a resource that must be synchronized
    // so that only one thread at a time can enter.
    private static void UseResource()
    {
        // Wait until it is safe to enter.
        mut.WaitOne();

        Console.WriteLine("{0} has entered the protected area", 
            Thread.CurrentThread.Name);

        // Place code to access non-reentrant resources here.

        // Simulate some work.
        Thread.Sleep(500);

        Console.WriteLine("{0} is leaving the protected area\r\n", 
            Thread.CurrentThread.Name);
         
        // Release the Mutex.
        mut.ReleaseMutex();
    }
}
// The example displays output like the following:
//       Creating thread owns the Mutex.
//       Creating thread releases the Mutex.
//       
//       Thread1 has entered the protected area
//       Thread1 is leaving the protected area
//       
//       Thread2 has entered the protected area
//       Thread2 is leaving the protected area
//       
//       Thread3 has entered the protected area
//       Thread3 is leaving the protected area
Imports System.Threading

Class Test
    ' Create a new Mutex. The creating thread owns the
    ' Mutex.
    Private Shared mut As New Mutex(True)
    Private Const numIterations As Integer = 1
    Private Const numThreads As Integer = 3

    <MTAThread> _
    Shared Sub Main()
        ' Create the threads that will use the protected resource.
        Dim i As Integer
        For i = 1 To numThreads
            Dim myThread As New Thread(AddressOf MyThreadProc)
            myThread.Name = [String].Format("Thread{0}", i)
            myThread.Start()
        Next i

        ' Wait one second before allowing other threads to
        ' acquire the Mutex.
        Console.WriteLine("Creating thread owns the Mutex.")
        Thread.Sleep(1000)

        Console.WriteLine("Creating thread releases the Mutex." & vbCrLf)
        mut.ReleaseMutex()
    End Sub

    Private Shared Sub MyThreadProc()
        Dim i As Integer
        For i = 1 To numIterations
            UseResource()
        Next i
    End Sub

    ' This method represents a resource that must be synchronized
    ' so that only one thread at a time can enter.
    Private Shared Sub UseResource()
        ' Wait until it is safe to enter.
        mut.WaitOne()

        Console.WriteLine("{0} has entered protected area", _
            Thread.CurrentThread.Name)

        ' Place code to access non-reentrant resources here.

        ' Simulate some work
        Thread.Sleep(500)

        Console.WriteLine("{0} is leaving protected area" & vbCrLf, _
            Thread.CurrentThread.Name)

        ' Release Mutex.
        mut.ReleaseMutex()
    End Sub
End Class
' The example displays output like the following:
'       Creating thread owns the Mutex.
'       Creating thread releases the Mutex.
'       
'       Thread1 has entered the protected area
'       Thread1 is leaving the protected area
'       
'       Thread2 has entered the protected area
'       Thread2 is leaving the protected area
'       
'       Thread3 has entered the protected area
'       Thread3 is leaving the protected area

另請參閱

適用於

Mutex(Boolean, String)

來源:
Mutex.cs
來源:
Mutex.cs
來源:
Mutex.cs

使用布林值,初始化 Mutex 類別的新執行個體,指出呼叫執行緒是否應該具有 Mutex 的初始擁有權,並擁有為 Mutex 名稱的字串。

public:
 Mutex(bool initiallyOwned, System::String ^ name);
[System.Security.SecurityCritical]
public Mutex (bool initiallyOwned, string name);
public Mutex (bool initiallyOwned, string? name);
public Mutex (bool initiallyOwned, string name);
[<System.Security.SecurityCritical>]
new System.Threading.Mutex : bool * string -> System.Threading.Mutex
new System.Threading.Mutex : bool * string -> System.Threading.Mutex
Public Sub New (initiallyOwned As Boolean, name As String)

參數

initiallyOwned
Boolean

true 表示如果這個呼叫的結果建立了具名系統 Mutex,則將具名系統 Mutex 的初始擁有權授與呼叫執行緒,否則為 false

name
String

如果同步物件要與其他處理序共用,則為名稱;否則為 null 或空字串。 名稱區分大小寫。 (\) 保留反斜杠字元,而且只能用來指定命名空間。 如需命名空間的詳細資訊,請參閱一節。 視操作系統而定,名稱可能會有進一步的限制。 例如,在 Unix 作業系統上,排除命名空間之後的名稱必須是有效的檔名。

屬性

例外狀況

具名 Mutex 已存在,並且具有存取控制安全性,但使用者沒有 FullControl

name 無效。 這可能基於數種原因,其中包括可能由作業系統設定的一些限制,例如未知前置詞或無效字元。 請注意,名稱和通用前置詞 「Global\」 和 「Local\」 會區分大小寫。

-或-

發生一些其他錯誤。 HResult 屬性可提供詳細資訊。

僅限 Windows:name 已指定未知命名空間。 請參閱物件名稱 \(英文\) 以取得詳細資訊。

name 太長。 長度限制可能會取決於作業系統或設定。

無法建立具有所提供 name 的同步物件。 不同類型的同步物件可能具有相同名稱。

僅限 .NET Framework:name 的長度超過 MAX_PATH (260 個字元)。

範例

下列範例示範如何使用具名 Mutex 在兩個不同的進程中執行的線程之間發出訊號。

從兩個或多個命令視窗執行此程式。 每個進程都會 Mutex 建立 代表具名 mutex MyMutex的物件。 具名 Mutex 是一個系統物件,其存留期是由代表它的物件存 Mutex 留期所系結。 當第一個進程建立其 Mutex 物件時,就會建立具名 mutex;在此範例中,具名 mutex 是由執行程式的第一個進程所擁有。 當所有 Mutex 代表它的物件都已釋放時,就會終結具名 Mutex。

此範例中使用的建構函式多載無法告訴呼叫線程是否已授與具名 Mutex 的初始擁有權。 除非您確定線程將建立具名 Mutex,否則不應該使用此建構函式來要求初始擁有權。

using namespace System;
using namespace System::Threading;

int 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.
   Mutex^ m = gcnew 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();
}
using System;
using System.Threading;

public class Test1
{
    public static void 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.
        Mutex m = 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();
    }
}
Imports System.Threading

Public Class Test
   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 
End Class

備註

name前面可能會加上 Global\Local\ ,以指定命名空間。 Global指定命名空間時,同步處理物件可能會與系統上的任何進程共用。 Local指定命名空間時,這也是未指定命名空間時的預設值,同步處理物件可能會與相同會話中的進程共用。 在 Windows 上,會話是登入工作階段,而服務通常會在不同的非互動式會話中執行。 在類似 Unix 的作業系統上,每個殼層都有自己的會話。 會話本機同步處理物件可能適用於在進程與父子關聯性之間同步處理,而它們全都在相同的會話中執行。 如需 Windows 上同步處理物件名稱的詳細資訊,請參閱 物件名稱

如果 已提供 , name 且要求之型別的同步處理物件已存在於 命名空間中,則會使用現有的同步處理物件。 如果命名空間中已經有不同類型的同步處理物件, WaitHandleCannotBeOpenedException 則會擲回 。 否則,會建立新的同步處理物件。

如果 name 不是 nullinitiallyOwnedtrue,則只有在建立具名系統 Mutex 時,呼叫線程才會擁有 mutex,因為此呼叫的結果。 由於沒有機制可用來判斷是否已建立具名系統 Mutex,因此最好在呼叫這個建構函式多載時指定 falseinitiallyOwned 。 如果您需要判斷初始擁有權,可以使用 建 Mutex(Boolean, String, Boolean) 構函式。

這個建構函式會 Mutex 初始化 物件,此物件表示具名系統 Mutex。 您可以建立多個 Mutex 代表相同具名系統 Mutex 的物件。

如果已使用訪問控制安全性建立具名 Mutex,而且呼叫端沒有 MutexRights.FullControl,則會擲回例外狀況。 若要開啟只有同步處理線程活動所需的許可權的現有具名 Mutex,請參閱 OpenExisting 方法。

如果您為 指定 null 或空字串 name,則會建立本機 Mutex,就像您已呼叫建 Mutex(Boolean) 構函式一樣。 在這裡情況下, createdNew 一律 true為 。

因為它們是全系統,所以具名 Mutex 可用來協調跨進程界限的資源使用。

注意

在執行終端機服務的伺服器上,具名系統 Mutex 可以有兩個層級的可見性。 如果名稱以前置詞 Global\開頭,則會在所有終端伺服器會話中看到 Mutex。 如果名稱以前置詞 Local\開頭,則 mutex 只會顯示在建立它的終端伺服器會話中。 在此情況下,具有相同名稱的個別 Mutex 可以存在於伺服器上的其他終端機伺服器會話中。 如果您在建立具名 Mutex 時未指定前置詞,它會採用前置詞 Local\。 在終端機伺服器會話內,兩個名稱只有其前置詞不同的 Mutex 是個別的 Mutex,而且終端伺服器會話中的所有進程都可以看見這兩者。 也就是說,前置詞名稱和Global\Local\描述與終端伺服器會話相對的 Mutex 名稱範圍,而不是相對於進程。

警告

根據預設,具名 mutex 不限於建立它的使用者。 其他使用者可能能夠開啟並使用 mutex,包括輸入 mutex 來干擾 mutex,而不是結束 Mutex。 在類似 Unix 的作業系統上,文件系統會用於具名 Mutex 的實作中,而其他使用者可能會以更顯著的方式干擾具名 Mutex。 在 Windows 上,若要限制特定使用者的存取權,您可以使用建構函式多載,或在 MutexAcl 建立具名 Mutex 時傳入 MutexSecurity 。 在類似 Unix 的作業系統上,目前無法限制對具名 Mutex 的存取。 避免在可能具有不受信任使用者執行程式碼的系統上使用具名 Mutex,而不受存取限制。

反斜杠 (\) 是 mutex 名稱中的保留字元。 請勿在 mutex 名稱中使用反斜杠 (\) ,除非在終端伺服器會話中使用 mutex 的附注中所指定。 否則,可能會擲回 DirectoryNotFoundException,即使是 Mutex 的名稱代表現有的檔案。

另請參閱

適用於

Mutex(Boolean, String, Boolean)

來源:
Mutex.cs
來源:
Mutex.cs
來源:
Mutex.cs

使用表示呼叫執行緒是否應該具有 Mutex 的初始擁有權的布林值、代表 Mutex 名稱的字串,以及當方法傳回時表示是否將 Mutex 的初始擁有權授與呼叫執行緒的布林值,初始化 Mutex 類別的新執行個體。

public:
 Mutex(bool initiallyOwned, System::String ^ name, [Runtime::InteropServices::Out] bool % createdNew);
[System.Security.SecurityCritical]
public Mutex (bool initiallyOwned, string name, out bool createdNew);
public Mutex (bool initiallyOwned, string? name, out bool createdNew);
public Mutex (bool initiallyOwned, string name, out bool createdNew);
[<System.Security.SecurityCritical>]
new System.Threading.Mutex : bool * string * bool -> System.Threading.Mutex
new System.Threading.Mutex : bool * string * bool -> System.Threading.Mutex
Public Sub New (initiallyOwned As Boolean, name As String, ByRef createdNew As Boolean)

參數

initiallyOwned
Boolean

true 表示如果這個呼叫的結果建立了具名系統 Mutex,則將具名系統 Mutex 的初始擁有權授與呼叫執行緒,否則為 false

name
String

如果同步物件要與其他處理序共用,則為名稱;否則為 null 或空字串。 名稱區分大小寫。 (\) 保留反斜杠字元,而且只能用來指定命名空間。 如需命名空間的詳細資訊,請參閱一節。 視操作系統而定,名稱可能會有進一步的限制。 例如,在 Unix 作業系統上,排除命名空間之後的名稱必須是有效的檔名。

createdNew
Boolean

當這個方法傳回時,如果已建立本機 Mutex (也就是說,如果 namenull 或空字串),或是已建立指定的具名系統 Mutex,則會包含 true 的布林值;如果指定的具名系統 Mutex 已存在,則為 false。 這個參數會以未初始化的狀態傳遞。

屬性

例外狀況

具名 Mutex 已存在,並且具有存取控制安全性,但使用者沒有 FullControl

name 無效。 這可能基於數種原因,其中包括可能由作業系統設定的一些限制,例如未知前置詞或無效字元。 請注意,名稱和通用前置詞 「Global\」 和 「Local\」 會區分大小寫。

-或-

發生一些其他錯誤。 HResult 屬性可提供詳細資訊。

僅限 Windows:name 已指定未知命名空間。 請參閱物件名稱 \(英文\) 以取得詳細資訊。

name 太長。 長度限制可能會取決於作業系統或設定。

無法建立具有所提供 name 的同步物件。 不同類型的同步物件可能具有相同名稱。

僅限 .NET Framework:name 的長度超過 MAX_PATH (260 個字元)。

範例

下列程式代碼範例示範如何使用具名 Mutex 在進程或線程之間發出訊號。 從兩個或多個命令視窗執行此程式。 每個進程都會 Mutex 建立代表具名 mutex “MyMutex” 的 物件。 具名 Mutex 是系統物件。 在此範例中,其存留期是由代表它的 物件的存 Mutex 留期所系結。 當第一個進程建立其本機 Mutex 物件時,就會建立具名 Mutex,並在表示它的所有 Mutex 物件都已釋放時終結。 具名 Mutex 最初是由第一個進程所擁有。 第二個進程和任何後續進程會等候先前的進程釋放具名 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 second process (and any subsequent process) waits for earlier
// processes to release the named mutex.
using namespace System;
using namespace System::Threading;
int main()
{
   
   // Set this variable to false if you do not want to request 
   // initial ownership of the named mutex.
   bool requestInitialOwnership = true;
   bool mutexWasCreated;
   
   // Request initial ownership of the named mutex by passing
   // true for the first parameter. Only one system object named 
   // "MyMutex" can exist; the local Mutex object represents 
   // this system object. If "MyMutex" is created by this call,
   // then mutexWasCreated contains true; otherwise, it contains
   // false.
   Mutex^ m = gcnew Mutex( requestInitialOwnership, "MyMutex", mutexWasCreated );
   
   // This thread owns the mutex only if it both requested 
   // initial ownership and created the named mutex. Otherwise,
   // it can request the named mutex by calling WaitOne.
   if (  !(requestInitialOwnership && mutexWasCreated) )
   {
      Console::WriteLine(  "Waiting for the named mutex." );
      m->WaitOne();
   }

   
   // Once the process has gained control of the named mutex,
   // hold onto it until the user presses ENTER.
   Console::WriteLine(  "This process owns the named mutex. "
    "Press ENTER to release the mutex and exit." );
   Console::ReadLine();
   
   // Call ReleaseMutex to allow other threads to gain control
   // of the named mutex. If you keep a reference to the local
   // Mutex, you can call WaitOne to request control of the 
   // named mutex.
   m->ReleaseMutex();
}
// 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 second process (and any subsequent process) waits for earlier
// processes to release the named mutex.

using System;
using System.Threading;

public class Test12
{
    public static void Main()
    {
        // Set this variable to false if you do not want to request 
        // initial ownership of the named mutex.
        bool requestInitialOwnership = true;
        bool mutexWasCreated;

        // Request initial ownership of the named mutex by passing
        // true for the first parameter. Only one system object named 
        // "MyMutex" can exist; the local Mutex object represents 
        // this system object. If "MyMutex" is created by this call,
        // then mutexWasCreated contains true; otherwise, it contains
        // false.
        Mutex m = new Mutex(requestInitialOwnership, 
                            "MyMutex", 
                            out mutexWasCreated);
        
        // This thread owns the mutex only if it both requested 
        // initial ownership and created the named mutex. Otherwise,
        // it can request the named mutex by calling WaitOne.
        if (!(requestInitialOwnership && mutexWasCreated))
        {
            Console.WriteLine("Waiting for the named mutex.");
            m.WaitOne();
        }

        // Once the process has gained control of the named mutex,
        // hold onto it until the user presses ENTER.
        Console.WriteLine("This process owns the named mutex. " +
            "Press ENTER to release the mutex and exit.");
        Console.ReadLine();

        // Call ReleaseMutex to allow other threads to gain control
        // of the named mutex. If you keep a reference to the local
        // Mutex, you can call WaitOne to request control of the 
        // named mutex.
        m.ReleaseMutex();
    }
}
' 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 second process (and any subsequent process) waits for earlier
' processes to release the named mutex.

Imports System.Threading

Public Class Test
   
   <MTAThread> _
   Public Shared Sub Main()
      ' Set this variable to false if you do not want to request 
      ' initial ownership of the named mutex.
      Dim requestInitialOwnership As Boolean = True
      Dim mutexWasCreated As Boolean
      
      ' Request initial ownership of the named mutex by passing
      ' true for the first parameter. Only one system object named 
      ' "MyMutex" can exist; the local Mutex object represents 
      ' this system object. If "MyMutex" is created by this call,
      ' then mutexWasCreated contains true; otherwise, it contains
      ' false.
      Dim m As New Mutex(requestInitialOwnership, "MyMutex", _
          mutexWasCreated)
      
      ' This thread owns the mutex only if it both requested 
      ' initial ownership and created the named mutex. Otherwise,
      ' it can request the named mutex by calling WaitOne.
      If Not (requestInitialOwnership And mutexWasCreated) Then
         Console.WriteLine("Waiting for the named mutex.")
         m.WaitOne()
      End If
      
      ' Once the process has gained control of the named mutex,
      ' hold onto it until the user presses ENTER.
      Console.WriteLine("This process owns the named mutex. " _
          & "Press ENTER to release the mutex and exit.")
      Console.ReadLine()
      
      ' Call ReleaseMutex to allow other threads to gain control
      ' of the named mutex. If you keep a reference to the local
      ' Mutex, you can call WaitOne to request control of the 
      ' named mutex.
      m.ReleaseMutex()
   End Sub
End Class

備註

name前面可能會加上 Global\Local\ ,以指定命名空間。 Global指定命名空間時,同步處理物件可能會與系統上的任何進程共用。 Local指定命名空間時,這也是未指定命名空間時的預設值,同步處理物件可能會與相同會話中的進程共用。 在 Windows 上,會話是登入工作階段,而服務通常會在不同的非互動式會話中執行。 在類似 Unix 的作業系統上,每個殼層都有自己的會話。 會話本機同步處理物件可能適用於在進程與父子關聯性之間同步處理,而它們全都在相同的會話中執行。 如需 Windows 上同步處理物件名稱的詳細資訊,請參閱 物件名稱

如果 已提供 , name 且要求之型別的同步處理物件已存在於 命名空間中,則會使用現有的同步處理物件。 如果命名空間中已經有不同類型的同步處理物件, WaitHandleCannotBeOpenedException 則會擲回 。 否則,會建立新的同步處理物件。

如果 name 不是 nullinitiallyOwnedtrue,則只有在 truecreatedNew 呼叫之後,呼叫線程才會擁有具名 mutex。 否則,線程可以藉由呼叫 WaitOne 方法來要求 Mutex。

這個建構函式會 Mutex 初始化 物件,此物件表示具名系統 Mutex。 您可以建立多個 Mutex 代表相同具名系統 Mutex 的物件。

如果已使用訪問控制安全性建立具名 Mutex,而且呼叫端沒有 MutexRights.FullControl 許可權,則會擲回例外狀況。 若要開啟只有同步處理線程活動所需的許可權的現有具名 Mutex,請參閱 OpenExisting 方法。

如果您為 指定 null 或空字串 name,則會建立本機 Mutex,就像您已呼叫建 Mutex(Boolean) 構函式一樣。 在這裡情況下, createdNew 一律 true為 。

因為它們是全系統,所以具名 Mutex 可用來協調跨進程界限的資源使用。

注意

在執行終端機服務的伺服器上,具名系統 Mutex 可以有兩個層級的可見性。 如果名稱以前置詞 Global\開頭,則會在所有終端伺服器會話中看到 Mutex。 如果名稱以前置詞 Local\開頭,則 mutex 只會顯示在建立它的終端伺服器會話中。 在此情況下,具有相同名稱的個別 Mutex 可以存在於伺服器上的其他終端機伺服器會話中。 如果您在建立具名 Mutex 時未指定前置詞,它會採用前置詞 Local\。 在終端機伺服器會話內,兩個名稱只有其前置詞不同的 Mutex 是個別的 Mutex,而且終端伺服器會話中的所有進程都可以看見這兩者。 也就是說,前置詞名稱和Global\Local\描述與終端伺服器會話相對的 Mutex 名稱範圍,而不是相對於進程。

警告

根據預設,具名 mutex 不限於建立它的使用者。 其他使用者可能能夠開啟並使用 mutex,包括輸入 mutex 來干擾 mutex,而不是結束 Mutex。 在類似 Unix 的作業系統上,文件系統會用於具名 Mutex 的實作中,而其他使用者可能會以更顯著的方式干擾具名 Mutex。 在 Windows 上,若要限制特定使用者的存取權,您可以使用建構函式多載,或在 MutexAcl 建立具名 Mutex 時傳入 MutexSecurity 。 在類似 Unix 的作業系統上,目前無法限制對具名 Mutex 的存取。 避免在可能具有不受信任使用者執行程式碼的系統上使用具名 Mutex,而不受存取限制。

反斜杠 (\) 是 mutex 名稱中的保留字元。 請勿在 mutex 名稱中使用反斜杠 (\) ,除非在終端伺服器會話中使用 mutex 的附注中所指定。 否則,可能會擲回 DirectoryNotFoundException,即使是 Mutex 的名稱代表現有的檔案。

另請參閱

適用於

Mutex(Boolean, String, Boolean, MutexSecurity)

使用表示呼叫執行緒是否應該具有 Mutex 的初始擁有權的布林值、代表 Mutex 名稱的字串、當這個方法傳回時表示是否將 Mutex 的初始擁有權授與呼叫執行緒的布林值,以及要套用至具名 Mutex 的存取控制安全性,初始化 Mutex 類別的新執行個體。

public:
 Mutex(bool initiallyOwned, System::String ^ name, [Runtime::InteropServices::Out] bool % createdNew, System::Security::AccessControl::MutexSecurity ^ mutexSecurity);
public Mutex (bool initiallyOwned, string name, out bool createdNew, System.Security.AccessControl.MutexSecurity mutexSecurity);
[System.Security.SecurityCritical]
public Mutex (bool initiallyOwned, string name, out bool createdNew, System.Security.AccessControl.MutexSecurity mutexSecurity);
new System.Threading.Mutex : bool * string * bool * System.Security.AccessControl.MutexSecurity -> System.Threading.Mutex
[<System.Security.SecurityCritical>]
new System.Threading.Mutex : bool * string * bool * System.Security.AccessControl.MutexSecurity -> System.Threading.Mutex
Public Sub New (initiallyOwned As Boolean, name As String, ByRef createdNew As Boolean, mutexSecurity As MutexSecurity)

參數

initiallyOwned
Boolean

true 表示如果這個呼叫的結果建立了具名系統 Mutex,則將具名系統 Mutex 的初始擁有權授與呼叫執行緒,否則為 false

name
String

如果同步物件要與其他處理序共用,則為名稱;否則為 null 或空字串。 名稱區分大小寫。 (\) 保留反斜杠字元,而且只能用來指定命名空間。 如需命名空間的詳細資訊,請參閱一節。 視操作系統而定,名稱可能會有進一步的限制。 例如,在 Unix 作業系統上,排除命名空間之後的名稱必須是有效的檔名。

createdNew
Boolean

當這個方法傳回時,如果已建立本機 Mutex (也就是說,如果 namenull 或空字串),或是已建立指定的具名系統 Mutex,則會包含 true 的布林值;如果指定的具名系統 Mutex 已存在,則為 false。 這個參數會以未初始化的狀態傳遞。

mutexSecurity
MutexSecurity

MutexSecurity 物件,代表要套用至具名系統 Mutex 的存取控制安全性。

屬性

例外狀況

name 無效。 這可能基於數種原因,其中包括可能由作業系統設定的一些限制,例如未知前置詞或無效字元。 請注意,名稱和通用前置詞 「Global\」 和 「Local\」 會區分大小寫。

-或-

發生一些其他錯誤。 HResult 屬性可提供詳細資訊。

僅限 Windows:name 已指定未知命名空間。 請參閱物件名稱 \(英文\) 以取得詳細資訊。

name 太長。 長度限制可能會取決於作業系統或設定。

具名 Mutex 已存在,並且具有存取控制安全性,但使用者沒有 FullControl

無法建立具有所提供 name 的同步物件。 不同類型的同步物件可能具有相同名稱。

僅限 .NET Framework:name 的長度超過 MAX_PATH (260 個字元)。

範例

下列程式代碼範例示範具名 Mutex 與存取控制安全性的跨進程行為。 此範例會 OpenExisting(String) 使用 方法多載來測試具名 mutex 是否存在。

如果 Mutex 不存在,則會使用初始擁有權和訪問控制安全性來建立,以拒絕目前使用者使用 mutex 的許可權,但授與讀取和變更 mutex 許可權的許可權。

如果您從兩個命令窗口執行編譯的範例,第二個複本會在呼叫 OpenExisting(String)時擲回存取違規例外狀況。 攔截到例外狀況,而此範例會 OpenExisting(String, MutexRights) 使用 方法多載來開啟 mutex,並具有讀取和變更許可權所需的許可權。

變更許可權之後,mutex 會以輸入和釋放它所需的許可權開啟。 如果您從第三個命令視窗執行已編譯的範例,它會使用新的許可權來執行。

using namespace System;
using namespace System::Threading;
using namespace System::Security::AccessControl;
using namespace System::Security::Permissions;

public ref class Example
{
public:
   [SecurityPermissionAttribute(SecurityAction::Demand,Flags=SecurityPermissionFlag::UnmanagedCode)]
   static void Main()
   {
      String^ mutexName = L"MutexExample4";

      Mutex^ m = nullptr;
      bool doesNotExist = false;
      bool unauthorized = false;
      
      // The value of this variable is set by the mutex
      // constructor. It is true if the named system mutex was
      // created, and false if the named mutex already existed.
      //
      bool mutexWasCreated = false;

      // Attempt to open the named mutex.
      try
      {
         // Open the mutex with (MutexRights.Synchronize |
         // MutexRights.Modify), to enter and release the
         // named mutex.
         //
         m = Mutex::OpenExisting( mutexName );
      }
      catch ( WaitHandleCannotBeOpenedException^ ) 
      {
         Console::WriteLine( L"Mutex does not exist." );
         doesNotExist = true;
      }
      catch ( UnauthorizedAccessException^ ex ) 
      {
         Console::WriteLine( L"Unauthorized access: {0}", ex->Message );
         unauthorized = true;
      }

      // There are three cases: (1) The mutex does not exist.
      // (2) The mutex exists, but the current user doesn't
      // have access. (3) The mutex exists and the user has
      // access.
      //
      if ( doesNotExist )
      {
         // The mutex does not exist, so create it.
         // Create an access control list (ACL) that denies the
         // current user the right to enter or release the
         // mutex, but allows the right to read and change
         // security information for the mutex.
         //
         String^ user = String::Concat( Environment::UserDomainName, L"\\",
            Environment::UserName );
         MutexSecurity^ mSec = gcnew MutexSecurity;

         MutexAccessRule^ rule = gcnew MutexAccessRule( user,
            static_cast<MutexRights>(
               MutexRights::Synchronize |
               MutexRights::Modify),
            AccessControlType::Deny );
         mSec->AddAccessRule( rule );

         rule = gcnew MutexAccessRule( user,
            static_cast<MutexRights>(
               MutexRights::ReadPermissions |
                MutexRights::ChangePermissions),
            AccessControlType::Allow );
         mSec->AddAccessRule( rule );
         
         // Create a Mutex object that represents the system
         // mutex named by the constant 'mutexName', with
         // initial ownership for this thread, and with the
         // specified security access. The Boolean value that
         // indicates creation of the underlying system object
         // is placed in mutexWasCreated.
         //
         m = gcnew Mutex( true,mutexName, mutexWasCreated,mSec );
         
         // If the named system mutex was created, it can be
         // used by the current instance of this program, even
         // though the current user is denied access. The current
         // program owns the mutex. Otherwise, exit the program.
         //
         if ( mutexWasCreated )
         {
            Console::WriteLine( L"Created the mutex." );
         }
         else
         {
            Console::WriteLine( L"Unable to create the mutex." );
            return;
         }
      }
      else if ( unauthorized )
      {
         // Open the mutex to read and change the access control
         // security. The access control security defined above
         // allows the current user to do this.
         //
         try
         {
            m = Mutex::OpenExisting( mutexName,
               static_cast<MutexRights>(
                  MutexRights::ReadPermissions |
                  MutexRights::ChangePermissions) );
            
            // Get the current ACL. This requires
            // MutexRights.ReadPermissions.
            MutexSecurity^ mSec = m->GetAccessControl();

            String^ user = String::Concat( Environment::UserDomainName,
               L"\\", Environment::UserName );
            
            // First, the rule that denied the current user
            // the right to enter and release the mutex must
            // be removed.
            MutexAccessRule^ rule = gcnew MutexAccessRule( user,
               static_cast<MutexRights>(
                  MutexRights::Synchronize |
                  MutexRights::Modify),
               AccessControlType::Deny );
            mSec->RemoveAccessRule( rule );
            
            // Now grant the user the correct rights.
            //
            rule = gcnew MutexAccessRule( user,
               static_cast<MutexRights>(
                  MutexRights::Synchronize |
                  MutexRights::Modify),
               AccessControlType::Allow );
            mSec->AddAccessRule( rule );
            
            // Update the ACL. This requires
            // MutexRights.ChangePermissions.
            m->SetAccessControl( mSec );

            Console::WriteLine( L"Updated mutex security." );
            
            // Open the mutex with (MutexRights.Synchronize
            // | MutexRights.Modify), the rights required to
            // enter and release the mutex.
            //
            m = Mutex::OpenExisting( mutexName );
         }
         catch ( UnauthorizedAccessException^ ex ) 
         {
            Console::WriteLine(
               L"Unable to change permissions: {0}", ex->Message );
            return;
         }
      }
      
      // If this program created the mutex, it already owns
      // the mutex.
      //
      if ( !mutexWasCreated )
      {
         // Enter the mutex, and hold it until the program
         // exits.
         //
         try
         {
            Console::WriteLine( L"Wait for the mutex." );
            m->WaitOne();
            Console::WriteLine( L"Entered the mutex." );
         }
         catch ( UnauthorizedAccessException^ ex ) 
         {
            Console::WriteLine( L"Unauthorized access: {0}",
               ex->Message );
         }
      }

      Console::WriteLine( L"Press the Enter key to exit." );
      Console::ReadLine();
      m->ReleaseMutex();
      m->Dispose();
   }
};

int main()
{
   Example::Main();
}
using System;
using System.Threading;
using System.Security.AccessControl;

internal class Example
{
    internal static void Main()
    {
        const string mutexName = "MutexExample4";

        Mutex m = null;
        bool doesNotExist = false;
        bool unauthorized = false;

        // The value of this variable is set by the mutex
        // constructor. It is true if the named system mutex was
        // created, and false if the named mutex already existed.
        //
        bool mutexWasCreated = false;

        // Attempt to open the named mutex.
        try
        {
            // Open the mutex with (MutexRights.Synchronize |
            // MutexRights.Modify), to enter and release the
            // named mutex.
            //
            m = Mutex.OpenExisting(mutexName);
        }
        catch(WaitHandleCannotBeOpenedException)
        {
            Console.WriteLine("Mutex does not exist.");
            doesNotExist = true;
        }
        catch(UnauthorizedAccessException ex)
        {
            Console.WriteLine("Unauthorized access: {0}", ex.Message);
            unauthorized = true;
        }

        // There are three cases: (1) The mutex does not exist.
        // (2) The mutex exists, but the current user doesn't 
        // have access. (3) The mutex exists and the user has
        // access.
        //
        if (doesNotExist)
        {
            // The mutex does not exist, so create it.

            // Create an access control list (ACL) that denies the
            // current user the right to enter or release the 
            // mutex, but allows the right to read and change
            // security information for the mutex.
            //
            string user = Environment.UserDomainName + "\\"
                + Environment.UserName;
            var mSec = new MutexSecurity();

            MutexAccessRule rule = new MutexAccessRule(user, 
                MutexRights.Synchronize | MutexRights.Modify, 
                AccessControlType.Deny);
            mSec.AddAccessRule(rule);

            rule = new MutexAccessRule(user, 
                MutexRights.ReadPermissions | MutexRights.ChangePermissions,
                AccessControlType.Allow);
            mSec.AddAccessRule(rule);

            // Create a Mutex object that represents the system
            // mutex named by the constant 'mutexName', with
            // initial ownership for this thread, and with the
            // specified security access. The Boolean value that 
            // indicates creation of the underlying system object
            // is placed in mutexWasCreated.
            //
            m = new Mutex(true, mutexName, out mutexWasCreated, mSec);

            // If the named system mutex was created, it can be
            // used by the current instance of this program, even 
            // though the current user is denied access. The current
            // program owns the mutex. Otherwise, exit the program.
            // 
            if (mutexWasCreated)
            {
                Console.WriteLine("Created the mutex.");
            }
            else
            {
                Console.WriteLine("Unable to create the mutex.");
                return;
            }
        }
        else if (unauthorized)
        {
            // Open the mutex to read and change the access control
            // security. The access control security defined above
            // allows the current user to do this.
            //
            try
            {
                m = Mutex.OpenExisting(mutexName, 
                    MutexRights.ReadPermissions | MutexRights.ChangePermissions);

                // Get the current ACL. This requires 
                // MutexRights.ReadPermissions.
                MutexSecurity mSec = m.GetAccessControl();
                
                string user = Environment.UserDomainName + "\\"
                    + Environment.UserName;

                // First, the rule that denied the current user 
                // the right to enter and release the mutex must
                // be removed.
                MutexAccessRule rule = new MutexAccessRule(user, 
                     MutexRights.Synchronize | MutexRights.Modify,
                     AccessControlType.Deny);
                mSec.RemoveAccessRule(rule);

                // Now grant the user the correct rights.
                // 
                rule = new MutexAccessRule(user, 
                    MutexRights.Synchronize | MutexRights.Modify,
                    AccessControlType.Allow);
                mSec.AddAccessRule(rule);

                // Update the ACL. This requires
                // MutexRights.ChangePermissions.
                m.SetAccessControl(mSec);

                Console.WriteLine("Updated mutex security.");

                // Open the mutex with (MutexRights.Synchronize 
                // | MutexRights.Modify), the rights required to
                // enter and release the mutex.
                //
                m = Mutex.OpenExisting(mutexName);
            }
            catch(UnauthorizedAccessException ex)
            {
                Console.WriteLine("Unable to change permissions: {0}",
                    ex.Message);
                return;
            }
        }

        // If this program created the mutex, it already owns
        // the mutex.
        //
        if (!mutexWasCreated)
        {
            // Enter the mutex, and hold it until the program
            // exits.
            //
            try
            {
                Console.WriteLine("Wait for the mutex.");
                m.WaitOne();
                Console.WriteLine("Entered the mutex.");
            }
            catch(UnauthorizedAccessException ex)
            {
                Console.WriteLine("Unauthorized access: {0}", ex.Message);
            }
        }

        Console.WriteLine("Press the Enter key to exit.");
        Console.ReadLine();
        m.ReleaseMutex();
        m.Dispose();
    }
}
Imports System.Threading
Imports System.Security.AccessControl

Friend Class Example

    <MTAThread> _
    Friend Shared Sub Main()
        Const mutexName As String = "MutexExample4"

        Dim m As Mutex = Nothing
        Dim doesNotExist as Boolean = False
        Dim unauthorized As Boolean = False

        ' The value of this variable is set by the mutex
        ' constructor. It is True if the named system mutex was
        ' created, and False if the named mutex already existed.
        '
        Dim mutexWasCreated As Boolean

        ' Attempt to open the named mutex.
        Try
            ' Open the mutex with (MutexRights.Synchronize Or
            ' MutexRights.Modify), to enter and release the
            ' named mutex.
            '
            m = Mutex.OpenExisting(mutexName)
        Catch ex As WaitHandleCannotBeOpenedException
            Console.WriteLine("Mutex does not exist.")
            doesNotExist = True
        Catch ex As UnauthorizedAccessException
            Console.WriteLine("Unauthorized access: {0}", ex.Message)
            unauthorized = True
        End Try

        ' There are three cases: (1) The mutex does not exist.
        ' (2) The mutex exists, but the current user doesn't 
        ' have access. (3) The mutex exists and the user has
        ' access.
        '
        If doesNotExist Then
            ' The mutex does not exist, so create it.

            ' Create an access control list (ACL) that denies the
            ' current user the right to enter or release the 
            ' mutex, but allows the right to read and change
            ' security information for the mutex.
            '
            Dim user As String = Environment.UserDomainName _ 
                & "\" & Environment.UserName
            Dim mSec As New MutexSecurity()

            Dim rule As New MutexAccessRule(user, _
                MutexRights.Synchronize Or MutexRights.Modify, _
                AccessControlType.Deny)
            mSec.AddAccessRule(rule)

            rule = New MutexAccessRule(user, _
                MutexRights.ReadPermissions Or _
                MutexRights.ChangePermissions, _
                AccessControlType.Allow)
            mSec.AddAccessRule(rule)

            ' Create a Mutex object that represents the system
            ' mutex named by the constant 'mutexName', with
            ' initial ownership for this thread, and with the
            ' specified security access. The Boolean value that 
            ' indicates creation of the underlying system object
            ' is placed in mutexWasCreated.
            '
            m = New Mutex(True, mutexName, mutexWasCreated, mSec)

            ' If the named system mutex was created, it can be
            ' used by the current instance of this program, even 
            ' though the current user is denied access. The current
            ' program owns the mutex. Otherwise, exit the program.
            ' 
            If mutexWasCreated Then
                Console.WriteLine("Created the mutex.")
            Else
                Console.WriteLine("Unable to create the mutex.")
                Return
            End If

        ElseIf unauthorized Then

            ' Open the mutex to read and change the access control
            ' security. The access control security defined above
            ' allows the current user to do this.
            '
            Try
                m = Mutex.OpenExisting(mutexName, _
                    MutexRights.ReadPermissions Or _
                    MutexRights.ChangePermissions)

                ' Get the current ACL. This requires 
                ' MutexRights.ReadPermissions.
                Dim mSec As MutexSecurity = m.GetAccessControl()
                
                Dim user As String = Environment.UserDomainName _ 
                    & "\" & Environment.UserName

                ' First, the rule that denied the current user 
                ' the right to enter and release the mutex must
                ' be removed.
                Dim rule As New MutexAccessRule(user, _
                    MutexRights.Synchronize Or MutexRights.Modify, _
                    AccessControlType.Deny)
                mSec.RemoveAccessRule(rule)

                ' Now grant the user the correct rights.
                ' 
                rule = New MutexAccessRule(user, _
                    MutexRights.Synchronize Or MutexRights.Modify, _
                    AccessControlType.Allow)
                mSec.AddAccessRule(rule)

                ' Update the ACL. This requires
                ' MutexRights.ChangePermissions.
                m.SetAccessControl(mSec)

                Console.WriteLine("Updated mutex security.")

                ' Open the mutex with (MutexRights.Synchronize 
                ' Or MutexRights.Modify), the rights required to
                ' enter and release the mutex.
                '
                m = Mutex.OpenExisting(mutexName)

            Catch ex As UnauthorizedAccessException
                Console.WriteLine("Unable to change permissions: {0}", _
                    ex.Message)
                Return
            End Try

        End If

        ' If this program created the mutex, it already owns
        ' the mutex.
        '
        If Not mutexWasCreated Then
            ' Enter the mutex, and hold it until the program
            ' exits.
            '
            Try
                Console.WriteLine("Wait for the mutex.")
                m.WaitOne()
                Console.WriteLine("Entered the mutex.")
            Catch ex As UnauthorizedAccessException
                Console.WriteLine("Unauthorized access: {0}", _
                    ex.Message)
            End Try
        End If

        Console.WriteLine("Press the Enter key to exit.")
        Console.ReadLine()
        m.ReleaseMutex()
        m.Dispose()
    End Sub 
End Class

備註

name前面可能會加上 Global\Local\ ,以指定命名空間。 Global指定命名空間時,同步處理物件可能會與系統上的任何進程共用。 Local指定命名空間時,這也是未指定命名空間時的預設值,同步處理物件可能會與相同會話中的進程共用。 在 Windows 上,會話是登入工作階段,而服務通常會在不同的非互動式會話中執行。 在類似 Unix 的作業系統上,每個殼層都有自己的會話。 會話本機同步處理物件可能適用於在進程與父子關聯性之間同步處理,而它們全都在相同的會話中執行。 如需 Windows 上同步處理物件名稱的詳細資訊,請參閱 物件名稱

如果 已提供 , name 且要求之型別的同步處理物件已存在於 命名空間中,則會使用現有的同步處理物件。 如果命名空間中已經有不同類型的同步處理物件, WaitHandleCannotBeOpenedException 則會擲回 。 否則,會建立新的同步處理物件。

如果 name 不是 nullinitiallyOwnedtrue,則只有在 truecreatedNew 呼叫之後,呼叫線程才會擁有具名 mutex。 否則,線程可以藉由呼叫 WaitOne 方法來要求 Mutex。

使用此建構函式,在建立具名系統 Mutex 時將存取控制安全性套用至具名系統 Mutex,以防止其他程式碼控制 mutex。

這個建構函式會 Mutex 初始化 物件,此物件表示具名系統 Mutex。 您可以建立多個 Mutex 代表相同具名系統 Mutex 的物件。

如果具名系統 Mutex 不存在,則會使用指定的訪問控制安全性來建立它。 如果具名 Mutex 存在,則會忽略指定的訪問控制安全性。

注意

即使拒絕或無法對目前使用者授與某些訪問許可權,呼叫端仍可完全控制新建立 Mutex 的物件 mutexSecurity 。 不過,如果目前的用戶嘗試取得另一個物件來代表同一個 Mutex 具名 Mutex,請使用建構函式或 OpenExisting 方法,套用 Windows 訪問控制安全性。

如果已使用訪問控制安全性建立具名 Mutex,而且呼叫端沒有 MutexRights.FullControl,則會擲回例外狀況。 若要開啟只有同步處理線程活動所需的許可權的現有具名 Mutex,請參閱 OpenExisting 方法。

如果您為 指定 null 或空字串 name,則會建立本機 Mutex,就像您已呼叫建 Mutex(Boolean) 構函式一樣。 在這裡情況下, createdNew 一律 true為 。

因為它們是全系統,所以具名 Mutex 可用來協調跨進程界限的資源使用。

注意

在執行終端機服務的伺服器上,具名系統 Mutex 可以有兩個層級的可見性。 如果名稱以前置詞 Global\開頭,則會在所有終端伺服器會話中看到 Mutex。 如果名稱以前置詞 Local\開頭,則 mutex 只會顯示在建立它的終端伺服器會話中。 在此情況下,具有相同名稱的個別 Mutex 可以存在於伺服器上的其他終端機伺服器會話中。 如果您在建立具名 Mutex 時未指定前置詞,它會採用前置詞 Local\。 在終端機伺服器會話內,兩個名稱只有其前置詞不同的 Mutex 是個別的 Mutex,而且終端伺服器會話中的所有進程都可以看見這兩者。 也就是說,前置詞名稱和Global\Local\描述與終端伺服器會話相對的 Mutex 名稱範圍,而不是相對於進程。

警告

根據預設,具名 mutex 不限於建立它的使用者。 其他使用者可能能夠開啟並使用 mutex,包括輸入 mutex 來干擾 mutex,而不是結束 Mutex。 若要限制特定使用者的存取權,您可以在建立具名 Mutex 時傳入 MutexSecurity 。 避免在可能具有不受信任使用者執行程式碼的系統上使用具名 Mutex,而不受存取限制。

反斜杠 (\) 是 mutex 名稱中的保留字元。 請勿在 mutex 名稱中使用反斜杠 (\) ,除非在終端伺服器會話中使用 mutex 的附注中所指定。 否則,可能會擲回 DirectoryNotFoundException,即使是 Mutex 的名稱代表現有的檔案。

適用於