Mutex-Konstruktor (Boolean, String)
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, sowie mit einer Zeichenfolge, die den Namen des Mutex darstellt.
Namespace: System.Threading
Assembly: mscorlib (in mscorlib.dll)
Syntax
'Declaration
Public Sub New ( _
initiallyOwned As Boolean, _
name As String _
)
'Usage
Dim initiallyOwned As Boolean
Dim name As String
Dim instance As New Mutex(initiallyOwned, name)
public Mutex (
bool initiallyOwned,
string name
)
public:
Mutex (
bool initiallyOwned,
String^ name
)
public Mutex (
boolean initiallyOwned,
String name
)
public function Mutex (
initiallyOwned : boolean,
name : String
)
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.
Ausnahmen
Ausnahmetyp | Bedingung |
---|---|
Der benannte Mutex ist vorhanden und verfügt über Zugriffssteuerungssicherheit, aber der Benutzer verfügt nicht über MutexRights.FullControl. |
|
Ein Win32-Fehler ist aufgetreten. |
|
Der benannte Mutex kann nicht erstellt werden, möglicherweise weil ein WaitHandle eines anderen Typs denselben Namen hat. |
|
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 Mutex nur, wenn der benannte Systemmutex als Ergebnis dieses Aufrufs erstellt wurde. Da kein Mechanismus vorhanden ist, mit dem bestimmt werden kann, ob ein benannter Systemmutex erstellt wurde, empfiehlt es sich bei einem Aufruf dieser Konstruktorüberladung, für initiallyOwnedfalse anzugeben. Sie können den Mutex(Boolean,String,Boolean)-Konstruktor verwenden, wenn Sie den ursprünglichen Besitz bestimmen müssen.
Dieser Konstruktor initialisiert ein Mutex-Objekt, der 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 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
' 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 constructor overload shown here cannot tell the calling thread
' whether initial ownership of the named mutex was granted. Therefore,
' do not request initial ownership unless you are certain that the
' thread will create the named mutex.
Imports System
Imports System.Threading
Public Class Test
<MTAThread> _
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 '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 constructor overload shown here cannot tell the calling thread
// whether initial ownership of the named mutex was granted. Therefore,
// do not request initial ownership unless you are certain that the
// thread will create the named mutex.
using System;
using System.Threading;
public class Test
{
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();
}
}
// 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 constructor overload shown here cannot tell the calling thread
// whether initial ownership of the named mutex was granted. Therefore,
// do not request initial ownership unless you are certain that the
// thread will create the named mutex.
using namespace System;
using namespace System::Threading;
int main()
{
// Create the named mutex. Only one system object named
// "MyMutex" can exist; the local Mutex object represents
// this system object, regardless of which process or thread
// caused "MyMutex" to be created.
Mutex^ m = gcnew Mutex( false,"MyMutex" );
// Try to gain control of the named mutex. If the mutex is
// controlled by another thread, wait for it to be released.
Console::WriteLine( "Waiting for the Mutex." );
m->WaitOne();
// Keep control of the mutex until the user presses
// ENTER.
Console::WriteLine( "This application owns the mutex. "
"Press ENTER to release the mutex and exit." );
Console::ReadLine();
m->ReleaseMutex();
}
// 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 constructor overload shown here cannot tell the calling thread
// whether initial ownership of the named mutex was granted. Therefore,
// do not request initial ownership unless you are certain that the
// thread will create the named mutex.
import System.*;
import System.Threading.*;
import System.Threading.Thread;
public class Test
{
public static void main(String[] args)
{
// 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();
} //main
} //Test
.NET Framework-Sicherheit
- SecurityPermissionAttribute zum Aufrufen von nicht verwaltetem Code, damit ein benannter Mutex erstellt werden kann. Zugeordnete Enumeration: SecurityPermissionFlag.UnmanagedCode.
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