Freigeben über


Mutex-Konstruktor (Boolean, String, Boolean)

Initialisiert eine neue Instanz der Mutex-Klasse mit einem booleschen Wert, der angibt, ob dem aufrufenden Thread der anfängliche Besitz des Mutex zugewiesen werden soll, mit einer Zeichenfolge mit dem Namen des Mutex sowie mit einem booleschen Wert, der beim Beenden der Methode angibt, ob dem aufrufenden Thread der anfängliche Besitz des Mutex gewährt wurde.

Namespace: System.Threading
Assembly: mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Sub New ( _
    initiallyOwned As Boolean, _
    name As String, _
    <OutAttribute> ByRef createdNew As Boolean _
)
'Usage
Dim initiallyOwned As Boolean
Dim name As String
Dim createdNew As Boolean

Dim instance As New Mutex(initiallyOwned, name, createdNew)
public Mutex (
    bool initiallyOwned,
    string name,
    out bool createdNew
)
public:
Mutex (
    bool initiallyOwned, 
    String^ name, 
    [OutAttribute] bool% createdNew
)
public Mutex (
    boolean initiallyOwned, 
    String name, 
    /** @attribute OutAttribute() */ /** @ref */ boolean createdNew
)
public function Mutex (
    initiallyOwned : boolean, 
    name : String, 
    createdNew : boolean
)

Parameter

  • initiallyOwned
    true, um dem aufrufenden Thread den anfänglichen Besitz des benannten Systemmutex zuzuweisen, wenn der benannte Systemmutex als Ergebnis dieses Aufrufs erstellt wird, andernfalls false.
  • name
    Der Name des Mutex. Bei einem Wert von NULL (Nothing in Visual Basic) ist das Mutex unbenannt.
  • createdNew
    Enthält beim Beenden dieser Methode einen booleschen Wert. Dieser ist true, wenn dem aufrufenden Thread der anfängliche Besitz des Mutex gewährt wurde, andernfalls false. Dieser Parameter wird nicht initialisiert übergeben.

Ausnahmen

Ausnahmetyp Bedingung

UnauthorizedAccessException

Der benannte Mutex ist vorhanden und verfügt über Zugriffssteuerungssicherheit, aber der Benutzer verfügt nicht über MutexRights.FullControl.

IOException

Ein Win32-Fehler ist aufgetreten.

ApplicationException

Der benannte Mutex kann nicht erstellt werden, möglicherweise weil ein WaitHandle eines anderen Typs denselben Namen hat.

ArgumentException

name ist länger als 260 Zeichen.

Hinweise

Wenn name nicht NULL (Nothing in Visual Basic) ist und initiallyOwnedtrue ist, besitzt der aufrufende Thread den benannten Mutex nur, wenn createdNew nach dem Aufruf true ist. Andernfalls kann der Thread den Mutex anfordern, indem er die WaitOne-Methode aufruft.

Dieser Konstruktor initialisiert ein Mutex-Objekt, das einen benannten Systemmutex darstellt. Sie können mehrere Mutex-Objekte erstellen, die denselben benannten Systemmutex darstellen.

Wenn der benannte Mutex bereits mit Zugriffssteuerungssicherheit erstellt wurde und der Aufrufer nicht über MutexRights.FullControl-Rechte verfügt, wird eine Ausnahme ausgelöst. Weitere Informationen zum Öffnen eines benannten Mutex mit nur den Berechtigungen, die für synchronisierte Threadaktivitäten erforderlich sind, finden Sie unter der OpenExisting-Methode.

Wenn Sie NULL (Nothing in Visual Basic) oder eine leere Zeichenfolge für name angeben, wird ein lokaler Mutex erstellt, wie bei einem Aufruf des Mutex(Boolean)-Konstruktors. In diesem Fall ist createdNew immer true.

Da sie systemweit gelten, kann mithilfe von benannten Mutexen die Ressourcenverwendung über Prozessgrenzen hinaus koordiniert werden.

Beispiel

Im folgenden Codebeispiel wird veranschaulicht, wie ein benannter Mutex zum Signalisieren zwischen Prozessen oder Threads verwendet wird. Führen Sie dieses Programm in mindestens zwei Befehlsfenstern aus. Jeder Prozess erstellt ein Mutex-Objekt, das den benannten Mutex "MyMutex" darstellt. Der benannte Mutex ist ein Systemobjekt. In diesem Beispiel wird seine Lebensdauer durch die Lebensdauer des Mutex-Objekts begrenzt, durch das er dargestellt wird. Der benannte Mutex wird erstellt, wenn der erste Prozess sein lokales Mutex-Objekt erstellt, und es wird zerstört, wenn alle Mutex-Objekte freigegeben wurden, durch die es dargestellt wird. Der benannte Mutex ist anfänglich im Besitz des ersten Prozesses. Der zweite und alle nachfolgenden Prozesse warten darauf, dass der benannte Mutex von vorangehenden Prozessen freigegeben wird.

' 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
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 'Main
End Class 'Test
// 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 Test
{
    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.
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.
import System.*;
import System.Threading.*;
import System.Threading.Thread;

public class Test
{
    public static void main(String[] args)
    {
        // Set this variable to false if you do not want to request 
        // initial ownership of the named mutex.
        boolean requestInitialOwnership = true;
        boolean mutexWasCreated = false;

        // 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", 
            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();
    } //main
} //Test

.NET Framework-Sicherheit

Plattformen

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

Siehe auch

Referenz

Mutex-Klasse
Mutex-Member
System.Threading-Namespace

Weitere Ressourcen

Verwaltetes Threading
Mutexe