SafeWaitHandle クラスとは? わかりやすく解説

SafeWaitHandle クラス

メモ : このクラスは、.NET Framework version 2.0新しく追加されたものです。

待機ハンドルラッパー クラス表します。このクラス継承できません。

名前空間: Microsoft.Win32.SafeHandles
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public NotInheritable Class
 SafeWaitHandle
    Inherits SafeHandleZeroOrMinusOneIsInvalid
Dim instance As SafeWaitHandle
public sealed class SafeWaitHandle : SafeHandleZeroOrMinusOneIsInvalid
public ref class SafeWaitHandle sealed : public
 SafeHandleZeroOrMinusOneIsInvalid
public final class SafeWaitHandle extends SafeHandleZeroOrMinusOneIsInvalid
public final class SafeWaitHandle extends
 SafeHandleZeroOrMinusOneIsInvalid
解説解説

SafeWaitHandle クラスは、System.Threading.WaitHandle クラスによって使用されます。これは Win32 ミューテックスおよび自動および手動リセット イベントラッパーです。

使用例使用例

相互運用機能使ってSafeWaitHandle クラスおよび CreateMutex アンマネージ関数使用してミューテックス作成する方法コード例次に示します

Imports System
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")> _
    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.GetLastWin32Error())
        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
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")]
    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.GetLastWin32Error());
        }
    }

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

}
継承階層継承階層
System.Object
   System.Runtime.ConstrainedExecution.CriticalFinalizerObject
     System.Runtime.InteropServices.SafeHandle
       Microsoft.Win32.SafeHandles.SafeHandleZeroOrMinusOneIsInvalid
        Microsoft.Win32.SafeHandles.SafeWaitHandle
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
SafeWaitHandle メンバ
Microsoft.Win32.SafeHandles 名前空間
SafeHandleZeroOrMinusOneIsInvalid クラス
WaitHandle
Mutex
AutoResetEvent
ManualResetEvent



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「SafeWaitHandle クラス」の関連用語

SafeWaitHandle クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



SafeWaitHandle クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS