SafeWaitHandle Klasa
Definicja
Reprezentuje klasę otoki dla dojścia oczekiwania.Represents a wrapper class for a wait handle.
public ref class SafeWaitHandle sealed : System::Runtime::InteropServices::SafeHandle
public ref class SafeWaitHandle sealed : Microsoft::Win32::SafeHandles::SafeHandleZeroOrMinusOneIsInvalid
[System.Security.SecurityCritical]
public sealed class SafeWaitHandle : System.Runtime.InteropServices.SafeHandle
public sealed class SafeWaitHandle : Microsoft.Win32.SafeHandles.SafeHandleZeroOrMinusOneIsInvalid
[System.Security.SecurityCritical]
public sealed class SafeWaitHandle : Microsoft.Win32.SafeHandles.SafeHandleZeroOrMinusOneIsInvalid
[<System.Security.SecurityCritical>]
type SafeWaitHandle = class
inherit SafeHandle
type SafeWaitHandle = class
inherit SafeHandleZeroOrMinusOneIsInvalid
[<System.Security.SecurityCritical>]
type SafeWaitHandle = class
inherit SafeHandleZeroOrMinusOneIsInvalid
Public NotInheritable Class SafeWaitHandle
Inherits SafeHandle
Public NotInheritable Class SafeWaitHandle
Inherits SafeHandleZeroOrMinusOneIsInvalid
- Dziedziczenie
- Dziedziczenie
- Atrybuty
Przykłady
Poniższy przykład kodu demonstruje, jak używać międzyoperacyjności do tworzenia obiektu mutex przy użyciu SafeWaitHandle klasy i funkcji niezarządzanej CreateMutex
.The following code example demonstrates how to use interop to create a mutex using the SafeWaitHandle class and the unmanaged CreateMutex
function.
using System;
using Microsoft.Win32.SafeHandles;
using System.Runtime.InteropServices;
class SafeHandlesExample
{
static void Main()
{
UnmanagedMutex uMutex = new UnmanagedMutex("YourCompanyName_SafeHandlesExample_MUTEX");
try
{
uMutex.Create();
Console.WriteLine("Mutex created. Press Enter to release it.");
Console.ReadLine();
}
catch (Exception e)
{
Console.WriteLine(e);
}
finally
{
uMutex.Release();
Console.WriteLine("Mutex Released.");
}
Console.ReadLine();
}
}
class UnmanagedMutex
{
// Use interop to call the CreateMutex function.
// For more information about CreateMutex,
// see the unmanaged MSDN reference library.
[DllImport("kernel32.dll", CharSet=CharSet.Unicode)]
static extern SafeWaitHandle CreateMutex(IntPtr lpMutexAttributes, bool bInitialOwner,
string lpName);
// Use interop to call the ReleaseMutex function.
// For more information about ReleaseMutex,
// see the unmanaged MSDN reference library.
[DllImport("kernel32.dll")]
public static extern bool ReleaseMutex(SafeWaitHandle hMutex);
private SafeWaitHandle handleValue = null;
private IntPtr mutexAttrValue = IntPtr.Zero;
private string nameValue = null;
public UnmanagedMutex(string Name)
{
nameValue = Name;
}
public void Create()
{
if (nameValue == null && nameValue.Length == 0)
{
throw new ArgumentNullException("nameValue");
}
handleValue = CreateMutex(mutexAttrValue,
true, nameValue);
// If the handle is invalid,
// get the last Win32 error
// and throw a Win32Exception.
if (handleValue.IsInvalid)
{
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
}
}
public SafeWaitHandle Handle
{
get
{
// If the handle is valid,
// return it.
if (!handleValue.IsInvalid)
{
return handleValue;
}
else
{
return null;
}
}
}
public string Name
{
get
{
return nameValue;
}
}
public void Release()
{
ReleaseMutex(handleValue);
}
}
Imports Microsoft.Win32.SafeHandles
Imports System.Runtime.InteropServices
Class SafeHandlesExample
Shared Sub Main()
Dim uMutex As New UnmanagedMutex("YourCompanyName_SafeHandlesExample_MUTEX")
Try
uMutex.Create()
Console.WriteLine("Mutex created. Press Enter to release it.")
Console.ReadLine()
Catch e As Exception
Console.WriteLine(e)
Finally
uMutex.Release()
Console.WriteLine("Mutex Released.")
End Try
Console.ReadLine()
End Sub
End Class
Class UnmanagedMutex
' Use interop to call the CreateMutex function.
' For more information about CreateMutex,
' see the unmanaged MSDN reference library.
<DllImport("kernel32.dll", CharSet:=CharSet.Unicode)> _
Shared Function CreateMutex(ByVal lpMutexAttributes As IntPtr, ByVal bInitialOwner As Boolean, ByVal lpName As String) As SafeWaitHandle
End Function
' Use interop to call the ReleaseMutex function.
' For more information about ReleaseMutex,
' see the unmanaged MSDN reference library.
<DllImport("kernel32.dll")> _
Public Shared Function ReleaseMutex(ByVal hMutex As SafeWaitHandle) As Boolean
End Function
Private handleValue As SafeWaitHandle = Nothing
Private mutexAttrValue As IntPtr = IntPtr.Zero
Private nameValue As String = Nothing
Public Sub New(ByVal Name As String)
nameValue = Name
End Sub
Public Sub Create()
If nameValue Is Nothing AndAlso nameValue.Length = 0 Then
Throw New ArgumentNullException("nameValue")
End If
handleValue = CreateMutex(mutexAttrValue, True, nameValue)
' If the handle is invalid,
' get the last Win32 error
' and throw a Win32Exception.
If handleValue.IsInvalid Then
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error())
End If
End Sub
Public ReadOnly Property Handle() As SafeWaitHandle
Get
' If the handle is valid,
' return it.
If Not handleValue.IsInvalid Then
Return handleValue
Else
Return Nothing
End If
End Get
End Property
Public ReadOnly Property Name() As String
Get
Return nameValue
End Get
End Property
Public Sub Release()
ReleaseMutex(handleValue)
End Sub
End Class
Uwagi
SafeWaitHandleKlasa jest używana przez System.Threading.WaitHandle klasę.The SafeWaitHandle class is used by the System.Threading.WaitHandle class. Jest to otoka dla muteksów Win32 i zdarzeń resetowania autoi ręcznej.It is a wrapper for Win32 mutexes and auto and manual reset events.
Ważne
Ten typ implementuje IDisposable interfejs.This type implements the IDisposable interface. Po zakończeniu korzystania z typu należy usunąć jego wartość bezpośrednio lub pośrednio.When you have finished using the type, you should dispose of it either directly or indirectly. Aby usunąć typ bezpośrednio, wywołaj jego Dispose metodę w try
/ catch
bloku.To dispose of the type directly, call its Dispose method in a try
/catch
block. Aby usunąć go pośrednio, użyj konstrukcji języka, takiej jak using
(w języku C#) lub Using
(w Visual Basic).To dispose of it indirectly, use a language construct such as using
(in C#) or Using
(in Visual Basic). Aby uzyskać więcej informacji, zobacz sekcję "Używanie obiektu implementującego interfejs IDisposable" w temacie dotyczącym IDisposable interfejsu.For more information, see the "Using an Object that Implements IDisposable" section in the IDisposable interface topic.
Konstruktory
SafeWaitHandle(IntPtr, Boolean) |
Inicjuje nowe wystąpienie klasy SafeWaitHandle.Initializes a new instance of the SafeWaitHandle class. |
Pola
handle |
Określa uchwyt, który ma być opakowany.Specifies the handle to be wrapped. (Odziedziczone po SafeHandle) |
Właściwości
IsClosed |
Pobiera wartość wskazującą, czy dojście jest zamknięte.Gets a value indicating whether the handle is closed. (Odziedziczone po SafeHandle) |
IsInvalid |
Pobiera wartość wskazującą, czy dojście jest nieprawidłowe.Gets a value that indicates whether the handle is invalid. |
IsInvalid |
Pobiera wartość wskazującą, czy dojście jest nieprawidłowe.Gets a value that indicates whether the handle is invalid. (Odziedziczone po SafeHandleZeroOrMinusOneIsInvalid) |
Metody
Close() |
Oznacza dojście do zwalniania i zwalniania zasobów.Marks the handle for releasing and freeing resources. (Odziedziczone po SafeHandle) |
DangerousAddRef(Boolean) |
Ręcznie zwiększa licznik odwołań w SafeHandle wystąpieniach.Manually increments the reference counter on SafeHandle instances. (Odziedziczone po SafeHandle) |
DangerousGetHandle() |
Zwraca wartość handle pola.Returns the value of the handle field. (Odziedziczone po SafeHandle) |
DangerousRelease() |
Ręcznie zmniejsza licznik odwołań do SafeHandle wystąpienia.Manually decrements the reference counter on a SafeHandle instance. (Odziedziczone po SafeHandle) |
Dispose() |
Zwalnia wszystkie zasoby używane przez SafeHandle klasę.Releases all resources used by the SafeHandle class. (Odziedziczone po SafeHandle) |
Dispose(Boolean) |
Zwalnia niezarządzane zasoby używane przez klasę, SafeHandle określając, czy należy wykonać normalną operację Dispose.Releases the unmanaged resources used by the SafeHandle class specifying whether to perform a normal dispose operation. (Odziedziczone po SafeHandle) |
Equals(Object) |
Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.Determines whether the specified object is equal to the current object. (Odziedziczone po Object) |
GetHashCode() |
Służy jako domyślna funkcja skrótu.Serves as the default hash function. (Odziedziczone po Object) |
GetType() |
Pobiera Type bieżące wystąpienie.Gets the Type of the current instance. (Odziedziczone po Object) |
MemberwiseClone() |
Tworzy skróconą kopię bieżącego elementu Object .Creates a shallow copy of the current Object. (Odziedziczone po Object) |
ReleaseHandle() |
Gdy jest zastępowany w klasie pochodnej, wykonuje kod wymagany do zwolnienia dojścia.When overridden in a derived class, executes the code required to free the handle. (Odziedziczone po SafeHandle) |
SetHandle(IntPtr) |
Ustawia dojście do określonego istniejącego już dojścia.Sets the handle to the specified pre-existing handle. (Odziedziczone po SafeHandle) |
SetHandleAsInvalid() |
Oznacza dojście, które nie jest już używane.Marks a handle as no longer used. (Odziedziczone po SafeHandle) |
ToString() |
Zwraca ciąg reprezentujący bieżący obiekt.Returns a string that represents the current object. (Odziedziczone po Object) |