SafeWaitHandle Klasa

Definicja

Reprezentuje klasę otoki dla uchwytu oczekiwania.

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
SafeWaitHandle
Dziedziczenie
Atrybuty

Przykłady

W poniższym przykładzie kodu pokazano, jak za pomocą międzyoperacyjności utworzyć mutex przy użyciu SafeWaitHandle klasy i funkcji niezarządzanej CreateMutex .

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

Klasa SafeWaitHandle jest używana przez klasę System.Threading.WaitHandle . Jest to otoka dla mutexes Win32 i zdarzeń automatycznego i ręcznego resetowania.

Ważne

Ten typ implementuje IDisposable interfejs. Po zakończeniu korzystania z typu należy go usunąć bezpośrednio lub pośrednio. Aby usunąć typ bezpośrednio, wywołaj metodę Disposetry/catch w bloku. Aby usunąć go pośrednio, użyj konstrukcji językowej, takiej jak using (w języku C#) lub Using (w Visual Basic). Aby uzyskać więcej informacji, zobacz sekcję "Using an Object that Implements IDisposable" (Używanie obiektu implementujące protokół IDisposable) w temacie interfejsu IDisposable .

Konstruktory

SafeWaitHandle()

Tworzy element SafeWaitHandle.

SafeWaitHandle(IntPtr, Boolean)

Inicjuje nowe wystąpienie klasy SafeWaitHandle.

Pola

handle

Określa uchwyt, który ma być opakowany.

(Odziedziczone po SafeHandle)

Właściwości

IsClosed

Pobiera wartość wskazującą, czy uchwyt jest zamknięty.

(Odziedziczone po SafeHandle)
IsInvalid

Pobiera wartość wskazującą, czy uchwyt jest nieprawidłowy.

IsInvalid

Pobiera wartość wskazującą, czy uchwyt jest nieprawidłowy.

(Odziedziczone po SafeHandleZeroOrMinusOneIsInvalid)

Metody

Close()

Oznacza uchwyt do zwalniania i zwalniania zasobów.

(Odziedziczone po SafeHandle)
DangerousAddRef(Boolean)

Ręczne zwiększanie licznika odwołania w SafeHandle wystąpieniach.

(Odziedziczone po SafeHandle)
DangerousGetHandle()

Zwraca wartość handle pola.

(Odziedziczone po SafeHandle)
DangerousRelease()

Ręczne dekrementuje licznik odwołań w wystąpieniu SafeHandle .

(Odziedziczone po SafeHandle)
Dispose()

Zwalnia wszystkie zasoby używane przez klasę SafeHandle .

(Odziedziczone po SafeHandle)
Dispose(Boolean)

Zwalnia niezarządzane zasoby używane przez klasę SafeHandle określającą, czy wykonać normalną operację usuwania.

(Odziedziczone po SafeHandle)
Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetType()

Type Pobiera bieżące wystąpienie.

(Odziedziczone po Object)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
ReleaseHandle()

Po zastąpieniu w klasie pochodnej wykonuje kod wymagany do zwolnienia uchwytu.

(Odziedziczone po SafeHandle)
SetHandle(IntPtr)

Ustawia uchwyt do określonego wstępnie istniejącego uchwytu.

(Odziedziczone po SafeHandle)
SetHandleAsInvalid()

Oznacza uchwyt jako nieużytny.

(Odziedziczone po SafeHandle)
ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)

Dotyczy

Zobacz też