Mutex Costruttori

Definizione

Inizializza una nuova istanza della classe Mutex.

Overload

Mutex()

Inizializza una nuova istanza della classe Mutex con le proprietà predefinite.

Mutex(Boolean)

Inizializza una nuova istanza della classe Mutex con un valore booleano che indica se il thread chiamante deve avere la proprietà iniziale del mutex.

Mutex(Boolean, String)

Inizializza una nuova istanza della classe Mutex con un valore booleano che indica se il thread chiamante deve avere la proprietà iniziale del mutex e con una stringa che rappresenta il nome del mutex.

Mutex(Boolean, String, Boolean)

Inizializza una nuova istanza della classe Mutex con un valore booleano che indica se il thread chiamante deve avere la proprietà iniziale del mutex, con una stringa che rappresenta il nome del mutex e con un valore booleano che, quando il metodo viene restituito, indichi se al thread chiamante era stata concessa la proprietà iniziale del mutex.

Mutex(Boolean, String, Boolean, MutexSecurity)

Inizializza una nuova istanza della classe Mutex con un valore booleano che indica se il thread chiamante deve avere la proprietà iniziale del mutex, con una stringa che rappresenta il nome del mutex, con una variabile Boolean che, quando il metodo viene restituito, indichi se al thread chiamante era stata concessa la proprietà iniziale del mutex e con la sicurezza del controllo di accesso da applicare al mutex denominato.

Mutex()

Inizializza una nuova istanza della classe Mutex con le proprietà predefinite.

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

Esempio

Nell'esempio di codice seguente viene illustrato come viene usato un oggetto locale Mutex per sincronizzare l'accesso a una risorsa protetta. Il thread che crea il mutex non è proprietario inizialmente.

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

Commenti

La chiamata a questo overload del costruttore equivale a chiamare l'overload del Mutex(Boolean) costruttore e specificare false per la proprietà iniziale del mutex. Ovvero, il thread chiamante non possiede il mutex.

Vedi anche

Si applica a

Mutex(Boolean)

Inizializza una nuova istanza della classe Mutex con un valore booleano che indica se il thread chiamante deve avere la proprietà iniziale del mutex.

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

Parametri

initiallyOwned
Boolean

Viene restituito true per concedere al thread chiamante la proprietà iniziale del mutex; in caso contrario, false.

Esempio

Nell'esempio di codice seguente viene illustrato come viene usato un oggetto locale Mutex per sincronizzare l'accesso a una risorsa protetta. Thread che ne crea il Mutex proprietario inizialmente.

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

Vedi anche

Si applica a

Mutex(Boolean, String)

Inizializza una nuova istanza della classe Mutex con un valore booleano che indica se il thread chiamante deve avere la proprietà iniziale del mutex e con una stringa che rappresenta il nome del 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)

Parametri

initiallyOwned
Boolean

true per concedere al thread chiamante la proprietà iniziale del mutex di sistema denominato, se questo è stato creato come risultato della chiamata; in caso contrario, false.

name
String

Il nome, se l'oggetto di sincronizzazione deve essere condiviso con altri processi; in caso contrario null o una stringa vuota. Per il nome è prevista la distinzione tra maiuscole e minuscole. Il carattere barra rovesciata (\) è riservato e può essere usato solo per specificare uno spazio dei nomi. Per altre informazioni sugli spazi dei nomi, vedere la sezione osservazioni. Potrebbero esserci ulteriori restrizioni per il nome a seconda del sistema operativo. Ad esempio, nei sistemi operativi basati su Unix, il nome dopo l'esclusione dello spazio dei nomi deve essere un nome file valido.

Attributi

Eccezioni

Il mutex denominato esiste e ha accesso alla sicurezza controllo, ma l'utente non dispone di FullControl.

name non è valido. I motivi possono essere diversi, e tra questi limitazioni implementate dal sistema operativo, come prefisso sconosciuto o caratteri non validi. Si noti che il nome e i prefissi comuni "Global\" e "Local\" fanno distinzione tra maiuscole e minuscole.

-oppure-

Si è verificato un altro errore. È possibile che la proprietà HResult offra ulteriori informazioni.

Solo Windows: name ha specificato uno spazio dei nomi sconosciuto. Per altre informazioni, vedere Object Names (Nomi oggetti).

name supera la lunghezza consentita. Le limitazioni di lunghezza possono dipendere dal sistema operativo o dalla configurazione.

Non è possibile creare un oggetto di sincronizzazione con l'elemento name specificato. Un oggetto di sincronizzazione di un tipo diverso potrebbe avere lo stesso nome.

Solo .NET Framework: la lunghezza di name supera MAX_PATH (260 caratteri).

Esempio

L'esempio seguente mostra come viene usato un mutex denominato per segnalare tra i thread in esecuzione in due processi distinti.

Eseguire questo programma da due o più finestre di comando. Ogni processo crea un Mutex oggetto che rappresenta il mutex MyMutexdenominato . Il mutex denominato è un oggetto di sistema la cui durata è vincolata dalla durata degli Mutex oggetti che lo rappresentano. Il mutex denominato viene creato quando il primo processo crea il relativo Mutex oggetto. In questo esempio il mutex denominato è di proprietà del primo processo che esegue il programma. Il mutex denominato viene eliminato definitivamente quando tutti gli Mutex oggetti che lo rappresentano sono stati rilasciati.

L'overload del costruttore usato in questo esempio non può indicare al thread chiamante se è stata concessa la proprietà iniziale del mutex denominato. È consigliabile non usare questo costruttore per richiedere la proprietà iniziale, a meno che non sia possibile assicurarsi che il thread crei il mutex denominato.

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

Commenti

Può name essere preceduto Global\ da o Local\ per specificare uno spazio dei nomi. Quando si specifica lo Global spazio dei nomi, l'oggetto di sincronizzazione può essere condiviso con tutti i processi nel sistema. Quando si specifica lo Local spazio dei nomi , che è anche l'impostazione predefinita quando non viene specificato alcuno spazio dei nomi, l'oggetto di sincronizzazione può essere condiviso con i processi nella stessa sessione. In Windows, una sessione è una sessione di accesso e i servizi vengono in genere eseguiti in una sessione diversa non interattiva. Nei sistemi operativi simili a Unix, ogni shell ha una sessione specifica. Gli oggetti di sincronizzazione locale della sessione possono essere appropriati per la sincronizzazione tra processi con una relazione padre/figlio in cui vengono eseguiti tutti nella stessa sessione. Per altre informazioni sui nomi degli oggetti di sincronizzazione in Windows, vedere Nomi di oggetti.

Se viene specificato un name oggetto e un oggetto di sincronizzazione del tipo richiesto esiste già nello spazio dei nomi, viene utilizzato l'oggetto di sincronizzazione esistente. Se nello spazio dei nomi esiste già un oggetto di sincronizzazione di un tipo diverso, viene generata un'eccezione WaitHandleCannotBeOpenedException . In caso contrario, viene creato un nuovo oggetto di sincronizzazione.

Se name non null è e initiallyOwned è true, il thread chiamante possiede il mutex solo se il mutex di sistema denominato è stato creato in seguito a questa chiamata. Poiché non esiste alcun meccanismo per determinare se è stato creato il mutex di sistema denominato, è preferibile specificare false per quando initiallyOwned si chiama questo overload del costruttore. È possibile usare il costruttore se è necessario determinare la Mutex(Boolean, String, Boolean) proprietà iniziale.

Questo costruttore inizializza un Mutex oggetto che rappresenta un mutex di sistema denominato. È possibile creare più Mutex oggetti che rappresentano lo stesso mutex di sistema denominato.

Se il mutex denominato è già stato creato con la sicurezza del controllo di accesso e il chiamante non ha MutexRights.FullControl, viene generata un'eccezione. Per aprire un mutex denominato esistente con solo le autorizzazioni necessarie per la sincronizzazione delle attività del thread, vedere il OpenExisting metodo .

Se si specifica null o una stringa vuota per name, viene creato un mutex locale, come se fosse stato chiamato il Mutex(Boolean) costruttore. In questo caso, createdNew è sempre true.

Poiché sono a livello di sistema, i mutex denominati possono essere usati per coordinare l'uso delle risorse attraverso i limiti del processo.

Nota

In un server che esegue Servizi terminal, un mutex di sistema denominato può avere due livelli di visibilità. Se il nome inizia con il prefisso Global\, il mutex è visibile in tutte le sessioni del server terminal. Se il nome inizia con il prefisso Local\, il mutex è visibile solo nella sessione del server terminal in cui è stato creato. In tal caso, un mutex separato con lo stesso nome può esistere in ognuna delle altre sessioni del server terminal nel server. Se non si specifica un prefisso quando si crea un mutex denominato, viene utilizzato il prefisso Local\. All'interno di una sessione del server terminal, due mutex i cui nomi differiscono solo per i relativi prefissi sono mutex separati ed entrambi sono visibili a tutti i processi nella sessione del server terminal. Ovvero, i nomi Global\ dei prefissi e Local\ descrivono l'ambito del nome mutex relativo alle sessioni del server terminal, non relative ai processi.

Attenzione

Per impostazione predefinita, un mutex denominato non è limitato all'utente che lo ha creato. Altri utenti possono essere in grado di aprire e usare il mutex, incluso interferire con il mutex immettendo il mutex e non chiudendolo. Nei sistemi operativi simili a Unix, il file system viene usato nell'implementazione di mutex denominati e altri utenti possono interferire con i mutex denominati in modi più significativi. In Windows, per limitare l'accesso a utenti specifici, è possibile usare un overload del costruttore o MutexAcl passare un oggetto MutexSecurity durante la creazione del mutex denominato. Nei sistemi operativi simili a Unix, attualmente non è possibile limitare l'accesso a un mutex denominato. Evitare di usare mutex denominati senza restrizioni di accesso nei sistemi che potrebbero avere utenti non attendibili che eseguono codice.

La barra rovesciata (\) è un carattere riservato in un nome mutex. Non usare una barra rovesciata (\) in un nome mutex, ad eccezione di quanto specificato nella nota sull'uso di mutex nelle sessioni del server terminal. In caso contrario, potrebbe essere generata una DirectoryNotFoundException, anche se il nome del mutex rappresenta un file esistente.

Vedi anche

Si applica a

Mutex(Boolean, String, Boolean)

Inizializza una nuova istanza della classe Mutex con un valore booleano che indica se il thread chiamante deve avere la proprietà iniziale del mutex, con una stringa che rappresenta il nome del mutex e con un valore booleano che, quando il metodo viene restituito, indichi se al thread chiamante era stata concessa la proprietà iniziale del 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)

Parametri

initiallyOwned
Boolean

true per concedere al thread chiamante la proprietà iniziale del mutex di sistema denominato, se questo è stato creato come risultato della chiamata; in caso contrario, false.

name
String

Il nome, se l'oggetto di sincronizzazione deve essere condiviso con altri processi; in caso contrario null o una stringa vuota. Per il nome è prevista la distinzione tra maiuscole e minuscole. Il carattere barra rovesciata (\) è riservato e può essere usato solo per specificare uno spazio dei nomi. Per altre informazioni sugli spazi dei nomi, vedere la sezione osservazioni. Potrebbero esserci ulteriori restrizioni per il nome a seconda del sistema operativo. Ad esempio, nei sistemi operativi basati su Unix, il nome dopo l'esclusione dello spazio dei nomi deve essere un nome file valido.

createdNew
Boolean

Quando questo metodo viene restituito, contiene un valore booleano che è true se è stato creato un mutex locale (ovvero, se il valore di name è null o una stringa vuota) oppure se è stato creato il mutex di sistema denominato specificato; false se il mutex di sistema denominato specificato è già esistente. Questo parametro viene passato non inizializzato.

Attributi

Eccezioni

Il mutex denominato esiste e ha accesso alla sicurezza controllo, ma l'utente non dispone di FullControl.

name non è valido. I motivi possono essere diversi, e tra questi limitazioni implementate dal sistema operativo, come prefisso sconosciuto o caratteri non validi. Si noti che il nome e i prefissi comuni "Global\" e "Local\" fanno distinzione tra maiuscole e minuscole.

-oppure-

Si è verificato un altro errore. È possibile che la proprietà HResult offra ulteriori informazioni.

Solo Windows: name ha specificato uno spazio dei nomi sconosciuto. Per altre informazioni, vedere Object Names (Nomi oggetti).

name supera la lunghezza consentita. Le limitazioni di lunghezza possono dipendere dal sistema operativo o dalla configurazione.

Non è possibile creare un oggetto di sincronizzazione con l'elemento name specificato. Un oggetto di sincronizzazione di un tipo diverso potrebbe avere lo stesso nome.

Solo .NET Framework: la lunghezza di name supera MAX_PATH (260 caratteri).

Esempio

Nell'esempio di codice seguente viene illustrato come viene usato un mutex denominato per segnalare tra processi o thread. Eseguire questo programma da due o più finestre di comando. Ogni processo crea un Mutex oggetto che rappresenta il mutex denominato "MyMutex". Il mutex denominato è un oggetto di sistema. In questo esempio la durata è vincolata dalle durate degli Mutex oggetti che lo rappresentano. Il mutex denominato viene creato quando il primo processo crea l'oggetto locale Mutex e viene eliminato definitivamente quando tutti gli Mutex oggetti che lo rappresentano sono stati rilasciati. Il mutex denominato è inizialmente di proprietà del primo processo. Il secondo processo e tutti i processi successivi attendono che i processi precedenti rilascino il mutex denominato.

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

Commenti

Può name essere preceduto Global\ da o Local\ per specificare uno spazio dei nomi. Quando si specifica lo Global spazio dei nomi, l'oggetto di sincronizzazione può essere condiviso con tutti i processi nel sistema. Quando si specifica lo Local spazio dei nomi , che è anche l'impostazione predefinita quando non viene specificato alcuno spazio dei nomi, l'oggetto di sincronizzazione può essere condiviso con i processi nella stessa sessione. In Windows, una sessione è una sessione di accesso e i servizi vengono in genere eseguiti in una sessione diversa non interattiva. Nei sistemi operativi simili a Unix, ogni shell ha una sessione specifica. Gli oggetti di sincronizzazione locale della sessione possono essere appropriati per la sincronizzazione tra processi con una relazione padre/figlio in cui vengono eseguiti tutti nella stessa sessione. Per altre informazioni sui nomi degli oggetti di sincronizzazione in Windows, vedere Nomi di oggetti.

Se viene specificato un name oggetto e un oggetto di sincronizzazione del tipo richiesto esiste già nello spazio dei nomi, viene utilizzato l'oggetto di sincronizzazione esistente. Se nello spazio dei nomi esiste già un oggetto di sincronizzazione di un tipo diverso, viene generata un'eccezione WaitHandleCannotBeOpenedException . In caso contrario, viene creato un nuovo oggetto di sincronizzazione.

Se name non null è e initiallyOwned è true, il thread chiamante possiede il mutex denominato solo se createdNew è true dopo la chiamata. In caso contrario, il thread può richiedere il mutex chiamando il WaitOne metodo .

Questo costruttore inizializza un Mutex oggetto che rappresenta un mutex di sistema denominato. È possibile creare più Mutex oggetti che rappresentano lo stesso mutex di sistema denominato.

Se il mutex denominato è già stato creato con la sicurezza del controllo di accesso e il chiamante non dispone MutexRights.FullControl dei diritti, viene generata un'eccezione. Per aprire un mutex denominato esistente con solo le autorizzazioni necessarie per la sincronizzazione delle attività del thread, vedere il OpenExisting metodo .

Se si specifica null o una stringa vuota per name, viene creato un mutex locale, come se fosse stato chiamato il Mutex(Boolean) costruttore. In questo caso, createdNew è sempre true.

Poiché sono a livello di sistema, i mutex denominati possono essere usati per coordinare l'uso delle risorse attraverso i limiti del processo.

Nota

In un server che esegue Servizi terminal, un mutex di sistema denominato può avere due livelli di visibilità. Se il nome inizia con il prefisso Global\, il mutex è visibile in tutte le sessioni del server terminal. Se il nome inizia con il prefisso Local\, il mutex è visibile solo nella sessione del server terminal in cui è stato creato. In tal caso, un mutex separato con lo stesso nome può esistere in ognuna delle altre sessioni del server terminal nel server. Se non si specifica un prefisso quando si crea un mutex denominato, viene utilizzato il prefisso Local\. All'interno di una sessione del server terminal, due mutex i cui nomi differiscono solo per i relativi prefissi sono mutex separati ed entrambi sono visibili a tutti i processi nella sessione del server terminal. Ovvero, i nomi Global\ dei prefissi e Local\ descrivono l'ambito del nome mutex relativo alle sessioni del server terminal, non relative ai processi.

Attenzione

Per impostazione predefinita, un mutex denominato non è limitato all'utente che lo ha creato. Altri utenti possono essere in grado di aprire e usare il mutex, incluso interferire con il mutex immettendo il mutex e non chiudendolo. Nei sistemi operativi simili a Unix, il file system viene usato nell'implementazione di mutex denominati e altri utenti possono interferire con i mutex denominati in modi più significativi. In Windows, per limitare l'accesso a utenti specifici, è possibile usare un overload del costruttore o MutexAcl passare un oggetto MutexSecurity durante la creazione del mutex denominato. Nei sistemi operativi simili a Unix, attualmente non è possibile limitare l'accesso a un mutex denominato. Evitare di usare mutex denominati senza restrizioni di accesso nei sistemi che potrebbero avere utenti non attendibili che eseguono codice.

La barra rovesciata (\) è un carattere riservato in un nome mutex. Non usare una barra rovesciata (\) in un nome mutex, ad eccezione di quanto specificato nella nota sull'uso di mutex nelle sessioni del server terminal. In caso contrario, potrebbe essere generata una DirectoryNotFoundException, anche se il nome del mutex rappresenta un file esistente.

Vedi anche

Si applica a

Mutex(Boolean, String, Boolean, MutexSecurity)

Inizializza una nuova istanza della classe Mutex con un valore booleano che indica se il thread chiamante deve avere la proprietà iniziale del mutex, con una stringa che rappresenta il nome del mutex, con una variabile Boolean che, quando il metodo viene restituito, indichi se al thread chiamante era stata concessa la proprietà iniziale del mutex e con la sicurezza del controllo di accesso da applicare al mutex denominato.

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)

Parametri

initiallyOwned
Boolean

true per concedere al thread chiamante la proprietà iniziale del mutex di sistema denominato, se questo è stato creato come risultato della chiamata; in caso contrario, false.

name
String

Il nome, se l'oggetto di sincronizzazione deve essere condiviso con altri processi; in caso contrario null o una stringa vuota. Per il nome è prevista la distinzione tra maiuscole e minuscole. Il carattere barra rovesciata (\) è riservato e può essere usato solo per specificare uno spazio dei nomi. Per altre informazioni sugli spazi dei nomi, vedere la sezione osservazioni. Potrebbero esserci ulteriori restrizioni per il nome a seconda del sistema operativo. Ad esempio, nei sistemi operativi basati su Unix, il nome dopo l'esclusione dello spazio dei nomi deve essere un nome file valido.

createdNew
Boolean

Quando questo metodo viene restituito, contiene un valore booleano che è true se è stato creato un mutex locale (ovvero, se il valore di name è null o una stringa vuota) oppure se è stato creato il mutex di sistema denominato specificato; false se il mutex di sistema denominato specificato è già esistente. Questo parametro viene passato non inizializzato.

mutexSecurity
MutexSecurity

Oggetto MutexSecurity che rappresenta la sicurezza del controllo di accesso da applicare al mutex di sistema denominato.

Attributi

Eccezioni

name non è valido. I motivi possono essere diversi, e tra questi limitazioni implementate dal sistema operativo, come prefisso sconosciuto o caratteri non validi. Si noti che il nome e i prefissi comuni "Global\" e "Local\" fanno distinzione tra maiuscole e minuscole.

-oppure-

Si è verificato un altro errore. È possibile che la proprietà HResult offra ulteriori informazioni.

Solo Windows: name ha specificato uno spazio dei nomi sconosciuto. Per altre informazioni, vedere Object Names (Nomi oggetti).

name supera la lunghezza consentita. Le limitazioni di lunghezza possono dipendere dal sistema operativo o dalla configurazione.

Il mutex denominato esiste e ha accesso alla sicurezza controllo, ma l'utente non dispone di FullControl.

Non è possibile creare un oggetto di sincronizzazione con l'elemento name specificato. Un oggetto di sincronizzazione di un tipo diverso potrebbe avere lo stesso nome.

Solo .NET Framework: la lunghezza di name supera MAX_PATH (260 caratteri).

Esempio

Nell'esempio di codice seguente viene illustrato il comportamento tra processi di un mutex denominato con sicurezza del controllo di accesso. Nell'esempio viene usato l'overload del OpenExisting(String) metodo per verificare l'esistenza di un mutex denominato.

Se il mutex non esiste, viene creato con la proprietà iniziale e la sicurezza del controllo di accesso che nega all'utente corrente il diritto di usare il mutex, ma concede il diritto di leggere e modificare le autorizzazioni per il mutex.

Se si esegue l'esempio compilato da due finestre di comando, la seconda copia genererà un'eccezione di violazione di accesso nella chiamata a OpenExisting(String). L'eccezione viene intercettata e l'esempio usa l'overload del OpenExisting(String, MutexRights) metodo per aprire il mutex con i diritti necessari per leggere e modificare le autorizzazioni.

Dopo aver modificato le autorizzazioni, il mutex viene aperto con i diritti necessari per immetterlo e rilasciarlo. Se si esegue l'esempio compilato da una terza finestra di comando, viene eseguito usando le nuove autorizzazioni.

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

Commenti

Può name essere preceduto Global\ da o Local\ per specificare uno spazio dei nomi. Quando si specifica lo Global spazio dei nomi, l'oggetto di sincronizzazione può essere condiviso con tutti i processi nel sistema. Quando si specifica lo Local spazio dei nomi , che è anche l'impostazione predefinita quando non viene specificato alcuno spazio dei nomi, l'oggetto di sincronizzazione può essere condiviso con i processi nella stessa sessione. In Windows, una sessione è una sessione di accesso e i servizi vengono in genere eseguiti in una sessione diversa non interattiva. Nei sistemi operativi simili a Unix, ogni shell ha una sessione specifica. Gli oggetti di sincronizzazione locale della sessione possono essere appropriati per la sincronizzazione tra processi con una relazione padre/figlio in cui vengono eseguiti tutti nella stessa sessione. Per altre informazioni sui nomi degli oggetti di sincronizzazione in Windows, vedere Nomi di oggetti.

Se viene specificato un name oggetto e un oggetto di sincronizzazione del tipo richiesto esiste già nello spazio dei nomi, viene utilizzato l'oggetto di sincronizzazione esistente. Se nello spazio dei nomi esiste già un oggetto di sincronizzazione di un tipo diverso, viene generata un'eccezione WaitHandleCannotBeOpenedException . In caso contrario, viene creato un nuovo oggetto di sincronizzazione.

Se name non null è e initiallyOwned è true, il thread chiamante possiede il mutex denominato solo se createdNew è true dopo la chiamata. In caso contrario, il thread può richiedere il mutex chiamando il WaitOne metodo .

Utilizzare questo costruttore per applicare la sicurezza del controllo di accesso a un mutex di sistema denominato al momento della creazione, impedendo ad altro codice di assumere il controllo del mutex.

Questo costruttore inizializza un Mutex oggetto che rappresenta un mutex di sistema denominato. È possibile creare più Mutex oggetti che rappresentano lo stesso mutex di sistema denominato.

Se il mutex di sistema denominato non esiste, viene creato con la sicurezza del controllo di accesso specificata. Se il mutex denominato esiste, la sicurezza del controllo di accesso specificata viene ignorata.

Nota

Il chiamante ha il controllo completo sull'oggetto appena creato Mutex anche se mutexSecurity nega o non concede alcuni diritti di accesso all'utente corrente. Tuttavia, se l'utente corrente tenta di ottenere un altro Mutex oggetto per rappresentare lo stesso mutex denominato, usando un costruttore o il metodo , viene applicata la sicurezza del OpenExisting controllo di accesso di Windows.

Se il mutex denominato è già stato creato con la sicurezza del controllo di accesso e il chiamante non ha MutexRights.FullControl, viene generata un'eccezione. Per aprire un mutex denominato esistente con solo le autorizzazioni necessarie per la sincronizzazione delle attività del thread, vedere il OpenExisting metodo .

Se si specifica null o una stringa vuota per name, viene creato un mutex locale, come se fosse stato chiamato il Mutex(Boolean) costruttore. In questo caso, createdNew è sempre true.

Poiché sono a livello di sistema, i mutex denominati possono essere usati per coordinare l'uso delle risorse attraverso i limiti del processo.

Nota

In un server che esegue Servizi terminal, un mutex di sistema denominato può avere due livelli di visibilità. Se il nome inizia con il prefisso Global\, il mutex è visibile in tutte le sessioni del server terminal. Se il nome inizia con il prefisso Local\, il mutex è visibile solo nella sessione del server terminal in cui è stato creato. In tal caso, un mutex separato con lo stesso nome può esistere in ognuna delle altre sessioni del server terminal nel server. Se non si specifica un prefisso quando si crea un mutex denominato, viene utilizzato il prefisso Local\. All'interno di una sessione del server terminal, due mutex i cui nomi differiscono solo per i relativi prefissi sono mutex separati ed entrambi sono visibili a tutti i processi nella sessione del server terminal. Ovvero, i nomi Global\ dei prefissi e Local\ descrivono l'ambito del nome mutex relativo alle sessioni del server terminal, non relative ai processi.

Attenzione

Per impostazione predefinita, un mutex denominato non è limitato all'utente che lo ha creato. Altri utenti possono essere in grado di aprire e usare il mutex, incluso interferire con il mutex immettendo il mutex e non chiudendolo. Per limitare l'accesso a utenti specifici, è possibile passare un oggetto MutexSecurity durante la creazione del mutex denominato. Evitare di usare mutex denominati senza restrizioni di accesso nei sistemi che potrebbero avere utenti non attendibili che eseguono codice.

La barra rovesciata (\) è un carattere riservato in un nome mutex. Non usare una barra rovesciata (\) in un nome mutex, ad eccezione di quanto specificato nella nota sull'uso di mutex nelle sessioni del server terminal. In caso contrario, potrebbe essere generata una DirectoryNotFoundException, anche se il nome del mutex rappresenta un file esistente.

Si applica a