Share via


Mutex Konstruktor

Definisi

Menginisialisasi instans baru kelas Mutex.

Overload

Mutex()

Menginisialisasi instans Mutex baru kelas dengan properti default.

Mutex(Boolean)

Menginisialisasi instans Mutex baru kelas dengan nilai Boolean yang menunjukkan apakah utas panggilan harus memiliki kepemilikan awal mutex.

Mutex(Boolean, String)

Menginisialisasi instans Mutex baru kelas dengan nilai Boolean yang menunjukkan apakah utas panggilan harus memiliki kepemilikan awal mutex, dan string yang merupakan nama mutex.

Mutex(Boolean, String, Boolean)

Menginisialisasi instans Mutex baru kelas dengan nilai Boolean yang menunjukkan apakah utas panggilan harus memiliki kepemilikan awal muteks, string yang merupakan nama muteks, dan nilai Boolean yang, ketika metode kembali, menunjukkan apakah utas panggilan diberikan kepemilikan awal muteks.

Mutex(Boolean, String, Boolean, MutexSecurity)

Menginisialisasi instans Mutex baru kelas dengan nilai Boolean yang menunjukkan apakah utas panggilan harus memiliki kepemilikan awal muteks, string yang merupakan nama muteks, variabel Boolean yang, ketika metode kembali, menunjukkan apakah utas panggilan diberikan kepemilikan awal muteks, dan keamanan kontrol akses yang akan diterapkan pada mutex bernama.

Mutex()

Sumber:
Mutex.cs
Sumber:
Mutex.cs
Sumber:
Mutex.cs

Menginisialisasi instans Mutex baru kelas dengan properti default.

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

Contoh

Contoh kode berikut menunjukkan bagaimana objek lokal Mutex digunakan untuk menyinkronkan akses ke sumber daya yang dilindungi. Alur yang membuat mutex awalnya tidak memilikinya.

// 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

Keterangan

Memanggil overload konstruktor ini sama dengan memanggil Mutex(Boolean) konstruktor kelebihan beban dan menentukan false kepemilikan awal mutex. Artinya, utas panggilan tidak memiliki mutex.

Lihat juga

Berlaku untuk

Mutex(Boolean)

Sumber:
Mutex.cs
Sumber:
Mutex.cs
Sumber:
Mutex.cs

Menginisialisasi instans Mutex baru kelas dengan nilai Boolean yang menunjukkan apakah utas panggilan harus memiliki kepemilikan awal mutex.

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

Parameter

initiallyOwned
Boolean

true untuk memberikan alur panggilan kepemilikan awal mutex; jika tidak, false.

Contoh

Contoh kode berikut menunjukkan bagaimana objek lokal Mutex digunakan untuk menyinkronkan akses ke sumber daya yang dilindungi. Utas yang membuat miliknya Mutex pada awalnya.

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

Lihat juga

Berlaku untuk

Mutex(Boolean, String)

Sumber:
Mutex.cs
Sumber:
Mutex.cs
Sumber:
Mutex.cs

Menginisialisasi instans Mutex baru kelas dengan nilai Boolean yang menunjukkan apakah utas panggilan harus memiliki kepemilikan awal mutex, dan string yang merupakan nama 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)

Parameter

initiallyOwned
Boolean

true untuk memberikan alur panggilan kepemilikan awal dari mutex sistem bernama jika mutex sistem bernama dibuat sebagai akibat dari panggilan ini; jika tidak, false.

name
String

Nama, jika objek sinkronisasi akan dibagikan dengan proses lain; jika tidak, null atau string kosong. Namanya peka huruf besar/kecil. Karakter garis miring terbalik (\) dicadangkan dan hanya dapat digunakan untuk menentukan namespace layanan. Untuk informasi selengkapnya tentang namespace layanan, lihat bagian keterangan. Mungkin ada pembatasan lebih lanjut pada nama tergantung pada sistem operasi. Misalnya, pada sistem operasi berbasis Unix, nama setelah mengecualikan namespace harus berupa nama file yang valid.

Atribut

Pengecualian

Mutex bernama ada dan memiliki keamanan kontrol akses, tetapi pengguna tidak memiliki FullControl.

name tidak valid. Ini bisa karena berbagai alasan, termasuk beberapa batasan yang mungkin ditempatkan oleh sistem operasi, seperti awalan yang tidak diketahui atau karakter yang tidak valid. Perhatikan bahwa nama dan awalan umum "Global\" dan "Local\" peka huruf besar/kecil.

-atau-

Ada beberapa kesalahan lainnya. Properti HResult dapat memberikan informasi lebih lanjut.

Hanya Windows: name menentukan namespace yang tidak dikenal. Lihat Nama Objek untuk informasi selengkapnya.

Terlalu name panjang. Pembatasan panjang dapat bergantung pada sistem operasi atau konfigurasi.

Objek sinkronisasi dengan yang disediakan name tidak dapat dibuat. Objek sinkronisasi dari jenis yang berbeda mungkin memiliki nama yang sama.

.NET Framework saja: name lebih panjang dari MAX_PATH (260 karakter).

Contoh

Contoh berikut menunjukkan bagaimana mutex bernama digunakan untuk memberi sinyal antara utas yang berjalan dalam dua proses terpisah.

Jalankan program ini dari dua jendela perintah atau lebih. Setiap proses membuat Mutex objek yang mewakili mutex MyMutexbernama . Mutex bernama adalah objek sistem yang masa pakainya dibatasi oleh masa Mutex pakai objek yang mewakilinya. Mutex bernama dibuat ketika proses pertama membuat objeknya Mutex ; dalam contoh ini, mutex bernama dimiliki oleh proses pertama yang menjalankan program. Mutex bernama dihancurkan ketika semua Mutex objek yang mewakilinya telah dirilis.

Kelebihan beban konstruktor yang digunakan dalam contoh ini tidak dapat memberi tahu utas panggilan apakah kepemilikan awal dari mutex bernama diberikan. Anda tidak boleh menggunakan konstruktor ini untuk meminta kepemilikan awal kecuali Anda dapat yakin bahwa utas akan membuat mutex bernama.

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

Keterangan

name mungkin diawali dengan Global\ atau Local\ untuk menentukan namespace. Global Ketika namespace ditentukan, objek sinkronisasi dapat dibagikan dengan proses apa pun pada sistem. Local Ketika namespace ditentukan, yang juga merupakan default ketika tidak ada namespace yang ditentukan, objek sinkronisasi dapat dibagikan dengan proses dalam sesi yang sama. Di Windows, sesi adalah sesi masuk, dan layanan biasanya berjalan dalam sesi non-interaktif yang berbeda. Pada sistem operasi seperti Unix, setiap shell memiliki sesinya sendiri. Objek sinkronisasi sesi-lokal mungkin sesuai untuk menyinkronkan antara proses dengan hubungan induk/anak di mana semuanya berjalan dalam sesi yang sama. Untuk informasi selengkapnya tentang nama objek sinkronisasi di Windows, lihat Nama Objek.

name Jika disediakan dan objek sinkronisasi dari jenis yang diminta sudah ada di namespace layanan, objek sinkronisasi yang ada akan digunakan. Jika objek sinkronisasi dari jenis yang berbeda sudah ada di namespace layanan, WaitHandleCannotBeOpenedException akan dilemparkan. Jika tidak, objek sinkronisasi baru dibuat.

Jika name tidak null dan initiallyOwned adalah true, utas panggilan memiliki mutex hanya jika mutex sistem bernama dibuat sebagai akibat dari panggilan ini. Karena tidak ada mekanisme untuk menentukan apakah mutex sistem bernama dibuat, lebih baik untuk initiallyOwned menentukan false kapan memanggil konstruktor ini kelebihan beban. Anda dapat menggunakan Mutex(Boolean, String, Boolean) konstruktor jika Anda perlu menentukan kepemilikan awal.

Konstruktor ini menginisialisasi Mutex objek yang mewakili mutex sistem bernama. Anda dapat membuat beberapa Mutex objek yang mewakili mutex sistem bernama yang sama.

Jika mutex bernama telah dibuat dengan keamanan kontrol akses, dan pemanggil tidak memiliki MutexRights.FullControl, pengecualian akan dilemparkan. Untuk membuka mutex bernama yang sudah ada hanya dengan izin yang diperlukan untuk menyinkronkan aktivitas utas, lihat OpenExisting metode .

Jika Anda menentukan null atau string kosong untuk name, mutex lokal dibuat, seolah-olah Anda telah memanggil Mutex(Boolean) konstruktor. Dalam hal ini, createdNew selalu true.

Karena di seluruh sistem, muteks bernama dapat digunakan untuk mengoordinasikan penggunaan sumber daya di seluruh batas proses.

Catatan

Pada server yang menjalankan Layanan Terminal, mutex sistem bernama dapat memiliki dua tingkat visibilitas. Jika namanya dimulai dengan awalan Global\, mutex terlihat di semua sesi server terminal. Jika namanya dimulai dengan awalan Local\, mutex hanya terlihat di sesi server terminal tempatnya dibuat. Dalam hal ini, mutex terpisah dengan nama yang sama dapat ada di setiap sesi server terminal lainnya di server. Jika Anda tidak menentukan awalan saat membuat mutex bernama, dibutuhkan awalan Local\. Dalam sesi server terminal, dua mutex yang namanya hanya berbeda dengan awalannya adalah mutex terpisah, dan keduanya terlihat oleh semua proses dalam sesi server terminal. Artinya, nama Global\ awalan dan Local\ menjelaskan cakupan nama mutex relatif terhadap sesi server terminal, tidak relatif terhadap proses.

Perhatian

Secara default, mutex bernama tidak dibatasi untuk pengguna yang membuatnya. Pengguna lain mungkin dapat membuka dan menggunakan mutex, termasuk mengganggu mutex dengan memasukkan mutex dan tidak keluar darinya. Pada sistem operasi seperti Unix, sistem file digunakan dalam implementasi mutex bernama, dan pengguna lain mungkin dapat mengganggu muteks bernama dengan cara yang lebih signifikan. Di Windows, untuk membatasi akses ke pengguna tertentu, Anda dapat menggunakan konstruktor kelebihan beban atau MutexAcl dan meneruskan MutexSecurity saat membuat mutex bernama. Pada sistem operasi seperti Unix, saat ini tidak ada cara untuk membatasi akses ke mutex bernama. Hindari menggunakan mutex bernama tanpa pembatasan akses pada sistem yang mungkin memiliki pengguna yang tidak tepercaya yang menjalankan kode.

Garis miring terbelakang (\) adalah karakter yang dicadangkan dalam nama mutex. Jangan gunakan garis miring terbalik (\) dalam nama mutex kecuali seperti yang ditentukan dalam catatan tentang menggunakan mutex di sesi server terminal. Jika tidak, DirectoryNotFoundException mungkin dilemparkan, meskipun nama mutex mewakili file yang ada.

Lihat juga

Berlaku untuk

Mutex(Boolean, String, Boolean)

Sumber:
Mutex.cs
Sumber:
Mutex.cs
Sumber:
Mutex.cs

Menginisialisasi instans Mutex baru kelas dengan nilai Boolean yang menunjukkan apakah utas panggilan harus memiliki kepemilikan awal muteks, string yang merupakan nama muteks, dan nilai Boolean yang, ketika metode kembali, menunjukkan apakah utas panggilan diberikan kepemilikan awal muteks.

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)

Parameter

initiallyOwned
Boolean

true untuk memberikan alur panggilan kepemilikan awal dari mutex sistem bernama jika mutex sistem bernama dibuat sebagai akibat dari panggilan ini; jika tidak, false.

name
String

Nama, jika objek sinkronisasi akan dibagikan dengan proses lain; jika tidak, null atau string kosong. Namanya peka huruf besar/kecil. Karakter garis miring terbalik (\) dicadangkan dan hanya dapat digunakan untuk menentukan namespace layanan. Untuk informasi selengkapnya tentang namespace layanan, lihat bagian keterangan. Mungkin ada pembatasan lebih lanjut pada nama tergantung pada sistem operasi. Misalnya, pada sistem operasi berbasis Unix, nama setelah mengecualikan namespace harus berupa nama file yang valid.

createdNew
Boolean

Ketika metode ini kembali, berisi Boolean yang true jika mutex lokal dibuat (yaitu, jika name adalah null atau string kosong) atau jika mutex sistem bernama yang ditentukan dibuat; false jika mutex sistem bernama yang ditentukan sudah ada. Parameter ini diteruskan tanpa diinisialisasi.

Atribut

Pengecualian

Mutex bernama ada dan memiliki keamanan kontrol akses, tetapi pengguna tidak memiliki FullControl.

name tidak valid. Ini bisa karena berbagai alasan, termasuk beberapa batasan yang mungkin ditempatkan oleh sistem operasi, seperti awalan yang tidak diketahui atau karakter yang tidak valid. Perhatikan bahwa nama dan awalan umum "Global\" dan "Local\" peka huruf besar/kecil.

-atau-

Ada beberapa kesalahan lainnya. Properti HResult dapat memberikan informasi lebih lanjut.

Hanya Windows: name menentukan namespace yang tidak dikenal. Lihat Nama Objek untuk informasi selengkapnya.

Terlalu name panjang. Pembatasan panjang dapat bergantung pada sistem operasi atau konfigurasi.

Objek sinkronisasi dengan yang disediakan name tidak dapat dibuat. Objek sinkronisasi dari jenis yang berbeda mungkin memiliki nama yang sama.

.NET Framework saja: name lebih panjang dari MAX_PATH (260 karakter).

Contoh

Contoh kode berikut menunjukkan bagaimana mutex bernama digunakan untuk memberi sinyal antara proses atau utas. Jalankan program ini dari dua jendela perintah atau lebih. Setiap proses membuat Mutex objek yang mewakili mutex bernama "MyMutex". Mutex bernama adalah objek sistem. Dalam contoh ini, masa pakainya dibatasi oleh masa Mutex pakai objek yang mewakilinya. Mutex bernama dibuat ketika proses pertama membuat objek lokalnya Mutex , dan dihancurkan ketika semua Mutex objek yang mewakilinya telah dirilis. Mutex bernama awalnya dimiliki oleh proses pertama. Proses kedua dan proses berikutnya menunggu proses sebelumnya untuk merilis mutex bernama.

// 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

Keterangan

name mungkin diawali dengan Global\ atau Local\ untuk menentukan namespace. Global Ketika namespace ditentukan, objek sinkronisasi dapat dibagikan dengan proses apa pun pada sistem. Local Ketika namespace ditentukan, yang juga merupakan default ketika tidak ada namespace yang ditentukan, objek sinkronisasi dapat dibagikan dengan proses dalam sesi yang sama. Di Windows, sesi adalah sesi masuk, dan layanan biasanya berjalan dalam sesi non-interaktif yang berbeda. Pada sistem operasi seperti Unix, setiap shell memiliki sesinya sendiri. Objek sinkronisasi sesi-lokal mungkin sesuai untuk menyinkronkan antara proses dengan hubungan induk/anak di mana semuanya berjalan dalam sesi yang sama. Untuk informasi selengkapnya tentang nama objek sinkronisasi di Windows, lihat Nama Objek.

name Jika disediakan dan objek sinkronisasi dari jenis yang diminta sudah ada di namespace layanan, objek sinkronisasi yang ada akan digunakan. Jika objek sinkronisasi dari jenis yang berbeda sudah ada di namespace layanan, WaitHandleCannotBeOpenedException akan dilemparkan. Jika tidak, objek sinkronisasi baru dibuat.

Jika name tidak null dan initiallyOwned adalah true, utas panggilan memiliki mutex bernama hanya jika createdNew setelah true panggilan. Jika tidak, utas dapat meminta mutex dengan memanggil WaitOne metode .

Konstruktor ini menginisialisasi Mutex objek yang mewakili mutex sistem bernama. Anda dapat membuat beberapa Mutex objek yang mewakili mutex sistem bernama yang sama.

Jika mutex bernama telah dibuat dengan keamanan kontrol akses, dan pemanggil tidak memiliki MutexRights.FullControl hak, pengecualian akan dilemparkan. Untuk membuka mutex bernama yang sudah ada hanya dengan izin yang diperlukan untuk menyinkronkan aktivitas utas, lihat OpenExisting metode .

Jika Anda menentukan null atau string kosong untuk name, mutex lokal dibuat, seolah-olah Anda telah memanggil Mutex(Boolean) konstruktor. Dalam hal ini, createdNew selalu true.

Karena di seluruh sistem, muteks bernama dapat digunakan untuk mengoordinasikan penggunaan sumber daya di seluruh batas proses.

Catatan

Pada server yang menjalankan Layanan Terminal, mutex sistem bernama dapat memiliki dua tingkat visibilitas. Jika namanya dimulai dengan awalan Global\, mutex terlihat di semua sesi server terminal. Jika namanya dimulai dengan awalan Local\, mutex hanya terlihat di sesi server terminal tempatnya dibuat. Dalam hal ini, mutex terpisah dengan nama yang sama dapat ada di setiap sesi server terminal lainnya di server. Jika Anda tidak menentukan awalan saat membuat mutex bernama, dibutuhkan awalan Local\. Dalam sesi server terminal, dua mutex yang namanya hanya berbeda dengan awalannya adalah mutex terpisah, dan keduanya terlihat oleh semua proses dalam sesi server terminal. Artinya, nama Global\ awalan dan Local\ menjelaskan cakupan nama mutex relatif terhadap sesi server terminal, tidak relatif terhadap proses.

Perhatian

Secara default, mutex bernama tidak dibatasi untuk pengguna yang membuatnya. Pengguna lain mungkin dapat membuka dan menggunakan mutex, termasuk mengganggu mutex dengan memasukkan mutex dan tidak keluar darinya. Pada sistem operasi seperti Unix, sistem file digunakan dalam implementasi mutex bernama, dan pengguna lain mungkin dapat mengganggu muteks bernama dengan cara yang lebih signifikan. Di Windows, untuk membatasi akses ke pengguna tertentu, Anda dapat menggunakan konstruktor kelebihan beban atau MutexAcl dan meneruskan MutexSecurity saat membuat mutex bernama. Pada sistem operasi seperti Unix, saat ini tidak ada cara untuk membatasi akses ke mutex bernama. Hindari menggunakan mutex bernama tanpa pembatasan akses pada sistem yang mungkin memiliki pengguna yang tidak tepercaya yang menjalankan kode.

Garis miring terbelakang (\) adalah karakter yang dicadangkan dalam nama mutex. Jangan gunakan garis miring terbalik (\) dalam nama mutex kecuali seperti yang ditentukan dalam catatan tentang menggunakan mutex di sesi server terminal. Jika tidak, DirectoryNotFoundException mungkin dilemparkan, meskipun nama mutex mewakili file yang ada.

Lihat juga

Berlaku untuk

Mutex(Boolean, String, Boolean, MutexSecurity)

Menginisialisasi instans Mutex baru kelas dengan nilai Boolean yang menunjukkan apakah utas panggilan harus memiliki kepemilikan awal muteks, string yang merupakan nama muteks, variabel Boolean yang, ketika metode kembali, menunjukkan apakah utas panggilan diberikan kepemilikan awal muteks, dan keamanan kontrol akses yang akan diterapkan pada mutex bernama.

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)

Parameter

initiallyOwned
Boolean

true untuk memberikan alur panggilan kepemilikan awal dari mutex sistem bernama jika mutex sistem bernama dibuat sebagai akibat dari panggilan ini; jika tidak, false.

name
String

Nama, jika objek sinkronisasi akan dibagikan dengan proses lain; jika tidak, null atau string kosong. Namanya peka huruf besar/kecil. Karakter garis miring terbalik (\) dicadangkan dan hanya dapat digunakan untuk menentukan namespace layanan. Untuk informasi selengkapnya tentang namespace layanan, lihat bagian keterangan. Mungkin ada pembatasan lebih lanjut pada nama tergantung pada sistem operasi. Misalnya, pada sistem operasi berbasis Unix, nama setelah mengecualikan namespace harus berupa nama file yang valid.

createdNew
Boolean

Ketika metode ini kembali, berisi Boolean yang true jika mutex lokal dibuat (yaitu, jika name adalah null atau string kosong) atau jika mutex sistem bernama yang ditentukan dibuat; false jika mutex sistem bernama yang ditentukan sudah ada. Parameter ini diteruskan tanpa diinisialisasi.

mutexSecurity
MutexSecurity

Objek MutexSecurity yang mewakili keamanan kontrol akses yang akan diterapkan ke mutex sistem bernama.

Atribut

Pengecualian

name tidak valid. Ini bisa karena berbagai alasan, termasuk beberapa batasan yang mungkin ditempatkan oleh sistem operasi, seperti awalan yang tidak diketahui atau karakter yang tidak valid. Perhatikan bahwa nama dan awalan umum "Global\" dan "Local\" peka huruf besar/kecil.

-atau-

Ada beberapa kesalahan lainnya. Properti HResult dapat memberikan informasi lebih lanjut.

Hanya Windows: name menentukan namespace yang tidak dikenal. Lihat Nama Objek untuk informasi selengkapnya.

Terlalu name panjang. Pembatasan panjang dapat bergantung pada sistem operasi atau konfigurasi.

Mutex bernama ada dan memiliki keamanan kontrol akses, tetapi pengguna tidak memiliki FullControl.

Objek sinkronisasi dengan yang disediakan name tidak dapat dibuat. Objek sinkronisasi dari jenis yang berbeda mungkin memiliki nama yang sama.

.NET Framework saja: name lebih panjang dari MAX_PATH (260 karakter).

Contoh

Contoh kode berikut menunjukkan perilaku lintas proses dari mutex bernama dengan keamanan kontrol akses. Contohnya menggunakan OpenExisting(String) metode kelebihan beban untuk menguji keberadaan mutex bernama.

Jika mutex tidak ada, itu dibuat dengan kepemilikan awal dan keamanan kontrol akses yang menolak pengguna saat ini hak untuk menggunakan mutex, tetapi memberikan hak untuk membaca dan mengubah izin pada mutex.

Jika Anda menjalankan contoh yang dikompilasi dari dua jendela perintah, salinan kedua akan melemparkan pengecualian pelanggaran akses pada panggilan ke OpenExisting(String). Pengecualian tertangkap, dan contohnya menggunakan OpenExisting(String, MutexRights) metode kelebihan beban untuk membuka mutex dengan hak yang diperlukan untuk membaca dan mengubah izin.

Setelah izin diubah, mutex dibuka dengan hak yang diperlukan untuk memasukkan dan melepaskannya. Jika Anda menjalankan contoh yang dikompilasi dari jendela perintah ketiga, contoh tersebut berjalan menggunakan izin baru.

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

Keterangan

name mungkin diawali dengan Global\ atau Local\ untuk menentukan namespace. Global Ketika namespace ditentukan, objek sinkronisasi dapat dibagikan dengan proses apa pun pada sistem. Local Ketika namespace ditentukan, yang juga merupakan default ketika tidak ada namespace yang ditentukan, objek sinkronisasi dapat dibagikan dengan proses dalam sesi yang sama. Di Windows, sesi adalah sesi masuk, dan layanan biasanya berjalan dalam sesi non-interaktif yang berbeda. Pada sistem operasi seperti Unix, setiap shell memiliki sesinya sendiri. Objek sinkronisasi sesi-lokal mungkin sesuai untuk menyinkronkan antara proses dengan hubungan induk/anak di mana semuanya berjalan dalam sesi yang sama. Untuk informasi selengkapnya tentang nama objek sinkronisasi di Windows, lihat Nama Objek.

name Jika disediakan dan objek sinkronisasi dari jenis yang diminta sudah ada di namespace layanan, objek sinkronisasi yang ada akan digunakan. Jika objek sinkronisasi dari jenis yang berbeda sudah ada di namespace layanan, WaitHandleCannotBeOpenedException akan dilemparkan. Jika tidak, objek sinkronisasi baru dibuat.

Jika name tidak null dan initiallyOwned adalah true, utas panggilan memiliki mutex bernama hanya jika createdNew setelah true panggilan. Jika tidak, utas dapat meminta mutex dengan memanggil WaitOne metode .

Gunakan konstruktor ini untuk menerapkan keamanan kontrol akses ke mutex sistem bernama saat dibuat, mencegah kode lain mengontrol mutex.

Konstruktor ini menginisialisasi Mutex objek yang mewakili mutex sistem bernama. Anda dapat membuat beberapa Mutex objek yang mewakili mutex sistem bernama yang sama.

Jika mutex sistem bernama tidak ada, mutex tersebut dibuat dengan keamanan kontrol akses yang ditentukan. Jika mutex bernama ada, keamanan kontrol akses yang ditentukan akan diabaikan.

Catatan

Pemanggil memiliki kontrol penuh atas objek yang baru dibuat Mutex meskipun mutexSecurity menolak atau gagal memberikan beberapa hak akses kepada pengguna saat ini. Namun, jika pengguna saat ini mencoba mendapatkan objek lain Mutex untuk mewakili mutex bernama yang sama, menggunakan konstruktor atau OpenExisting metode , keamanan kontrol akses Windows diterapkan.

Jika mutex bernama telah dibuat dengan keamanan kontrol akses, dan pemanggil tidak memiliki MutexRights.FullControl, pengecualian akan dilemparkan. Untuk membuka mutex bernama yang sudah ada hanya dengan izin yang diperlukan untuk menyinkronkan aktivitas utas, lihat OpenExisting metode .

Jika Anda menentukan null atau string kosong untuk name, mutex lokal dibuat, seolah-olah Anda telah memanggil Mutex(Boolean) konstruktor. Dalam hal ini, createdNew selalu true.

Karena di seluruh sistem, muteks bernama dapat digunakan untuk mengoordinasikan penggunaan sumber daya di seluruh batas proses.

Catatan

Pada server yang menjalankan Layanan Terminal, mutex sistem bernama dapat memiliki dua tingkat visibilitas. Jika namanya dimulai dengan awalan Global\, mutex terlihat di semua sesi server terminal. Jika namanya dimulai dengan awalan Local\, mutex hanya terlihat di sesi server terminal tempatnya dibuat. Dalam hal ini, mutex terpisah dengan nama yang sama dapat ada di setiap sesi server terminal lainnya di server. Jika Anda tidak menentukan awalan saat membuat mutex bernama, dibutuhkan awalan Local\. Dalam sesi server terminal, dua mutex yang namanya hanya berbeda dengan awalannya adalah mutex terpisah, dan keduanya terlihat oleh semua proses dalam sesi server terminal. Artinya, nama Global\ awalan dan Local\ menjelaskan cakupan nama mutex relatif terhadap sesi server terminal, tidak relatif terhadap proses.

Perhatian

Secara default, mutex bernama tidak dibatasi untuk pengguna yang membuatnya. Pengguna lain mungkin dapat membuka dan menggunakan mutex, termasuk mengganggu mutex dengan memasukkan mutex dan tidak keluar darinya. Untuk membatasi akses ke pengguna tertentu, Anda dapat meneruskan MutexSecurity saat membuat mutex bernama. Hindari menggunakan mutex bernama tanpa pembatasan akses pada sistem yang mungkin memiliki pengguna yang tidak tepercaya yang menjalankan kode.

Garis miring terbelakang (\) adalah karakter yang dicadangkan dalam nama mutex. Jangan gunakan garis miring terbalik (\) dalam nama mutex kecuali seperti yang ditentukan dalam catatan tentang menggunakan mutex di sesi server terminal. Jika tidak, DirectoryNotFoundException mungkin dilemparkan, meskipun nama mutex mewakili file yang ada.

Berlaku untuk