SafeWaitHandle 생성자

정의

오버로드

SafeWaitHandle()

SafeWaitHandle을 만듭니다.

SafeWaitHandle(IntPtr, Boolean)

SafeWaitHandle 클래스의 새 인스턴스를 초기화합니다.

SafeWaitHandle()

Source:
SafeWaitHandle.cs
Source:
SafeWaitHandle.cs
Source:
SafeWaitHandle.cs

SafeWaitHandle을 만듭니다.

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

적용 대상

SafeWaitHandle(IntPtr, Boolean)

Source:
SafeWaitHandle.cs
Source:
SafeWaitHandle.cs
Source:
SafeWaitHandle.cs

SafeWaitHandle 클래스의 새 인스턴스를 초기화합니다.

public:
 SafeWaitHandle(IntPtr existingHandle, bool ownsHandle);
public SafeWaitHandle (IntPtr existingHandle, bool ownsHandle);
new Microsoft.Win32.SafeHandles.SafeWaitHandle : nativeint * bool -> Microsoft.Win32.SafeHandles.SafeWaitHandle
Public Sub New (existingHandle As IntPtr, ownsHandle As Boolean)

매개 변수

existingHandle
IntPtr

nativeint

사용할 기존 핸들을 나타내는 IntPtr 개체입니다.

ownsHandle
Boolean

종료 단계에서 안정적으로 핸들을 해제하려면true 이고, 안정적으로 해제되지 않게 하려면 false 입니다(권장되지 않음).

예제

다음 코드 예제에서는 interop를 사용하여 클래스 및 관리 CreateMutex 되지 않는 함수를 SafeWaitHandle 사용하여 뮤텍스를 만드는 방법을 보여 줍니다.

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 IntPtr CreateMutex(IntPtr lpMutexAttributes, bool bInitialOwner,
   string lpName);

    [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");
        }

         IntPtr ptr = CreateMutex(mutexAttrValue,
                                        true, nameValue);

         handleValue = new SafeWaitHandle(ptr, true);

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

    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

        Dim ptr As IntPtr = CreateMutex(mutexAttrValue, True, nameValue)

        handleValue = New SafeWaitHandle(ptr, True)

        ' 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

추가 정보

적용 대상