diff --git a/snippets/csharp/Microsoft.Win32.SafeHandles/SafeWaitHandle/Overview/Project.csproj b/snippets/csharp/Microsoft.Win32.SafeHandles/SafeWaitHandle/Overview/Project.csproj index c02dc5044e7..ce312af21c1 100644 --- a/snippets/csharp/Microsoft.Win32.SafeHandles/SafeWaitHandle/Overview/Project.csproj +++ b/snippets/csharp/Microsoft.Win32.SafeHandles/SafeWaitHandle/Overview/Project.csproj @@ -1,8 +1,9 @@ - Library - net6.0 + Exe + net8.0 + true \ No newline at end of file diff --git a/snippets/csharp/Microsoft.Win32.SafeHandles/SafeWaitHandle/Overview/sample.cs b/snippets/csharp/Microsoft.Win32.SafeHandles/SafeWaitHandle/Overview/sample.cs index 14239f863b2..a96e1cb95c0 100644 --- a/snippets/csharp/Microsoft.Win32.SafeHandles/SafeWaitHandle/Overview/sample.cs +++ b/snippets/csharp/Microsoft.Win32.SafeHandles/SafeWaitHandle/Overview/sample.cs @@ -7,7 +7,7 @@ class SafeHandlesExample { static void Main() { - UnmanagedMutex uMutex = new UnmanagedMutex("YourCompanyName_SafeHandlesExample_MUTEX"); + UnmanagedMutex uMutex = new("YourCompanyName_SafeHandlesExample_MUTEX"); try { @@ -22,49 +22,43 @@ static void Main() finally { uMutex.Release(); - Console.WriteLine("Mutex Released."); + Console.WriteLine("Mutex released."); } - - Console.ReadLine(); } } -class UnmanagedMutex +partial class UnmanagedMutex(string Name) { - // 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); + [LibraryImport("kernel32.dll", EntryPoint = "CreateMutexW", StringMarshalling = StringMarshalling.Utf16)] + private static partial SafeWaitHandle CreateMutex( + IntPtr lpMutexAttributes, + [MarshalAs(UnmanagedType.Bool)] 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); + [LibraryImport("kernel32.dll")] + [return: MarshalAs(UnmanagedType.Bool)] + public static partial bool ReleaseMutex(SafeWaitHandle hMutex); - private SafeWaitHandle handleValue = null; - private IntPtr mutexAttrValue = IntPtr.Zero; - private string nameValue = null; - - public UnmanagedMutex(string Name) - { - nameValue = Name; - } + private SafeWaitHandle _handleValue = null; + private readonly IntPtr _mutexAttrValue = IntPtr.Zero; + private string nameValue = Name; public void Create() { ArgumentException.ThrowIfNullOrEmpty(nameValue); - handleValue = CreateMutex(mutexAttrValue, + _handleValue = CreateMutex(_mutexAttrValue, true, nameValue); // If the handle is invalid, // get the last Win32 error // and throw a Win32Exception. - if (handleValue.IsInvalid) + if (_handleValue.IsInvalid) { Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } @@ -74,11 +68,9 @@ public SafeWaitHandle Handle { get { - // If the handle is valid, - // return it. - if (!handleValue.IsInvalid) + if (!_handleValue.IsInvalid) { - return handleValue; + return _handleValue; } else { @@ -87,17 +79,11 @@ public SafeWaitHandle Handle } } - public string Name - { - get - { - return nameValue; - } - } + public string Name => nameValue; public void Release() { - ReleaseMutex(handleValue); + ReleaseMutex(_handleValue); } } // diff --git a/xml/Microsoft.Win32.SafeHandles/SafeWaitHandle.xml b/xml/Microsoft.Win32.SafeHandles/SafeWaitHandle.xml index cf51fc7e379..d28707ceb15 100644 --- a/xml/Microsoft.Win32.SafeHandles/SafeWaitHandle.xml +++ b/xml/Microsoft.Win32.SafeHandles/SafeWaitHandle.xml @@ -65,22 +65,22 @@ Represents a wrapper class for a wait handle. - class is used by the class. It is a wrapper for Win32 mutexes and auto and manual reset events. - + class is used by the class. It is a wrapper for Win32 mutexes and auto and manual reset events. + > [!IMPORTANT] -> This type implements the interface. When you have finished using the type, you should dispose of it either directly or indirectly. To dispose of the type directly, call its method in a `try`/`catch` block. To dispose of it indirectly, use a language construct such as `using` (in C#) or `Using` (in Visual Basic). For more information, see the "Using an Object that Implements IDisposable" section in the interface topic. - - - -## Examples - The following code example demonstrates how to use interop to create a mutex using the class and the unmanaged `CreateMutex` function. - +> This type implements the interface. When you have finished using the type, you should dispose of it either directly or indirectly. To dispose of the type directly, call its method in a `try`/`catch` block. To dispose of it indirectly, use a language construct such as `using` (in C#) or `Using` (in Visual Basic). + +## Examples + +The following code example demonstrates how to use interop to create a mutex using the class and the unmanaged `CreateMutex` function. + :::code language="csharp" source="~/snippets/csharp/Microsoft.Win32.SafeHandles/SafeWaitHandle/Overview/sample.cs" id="Snippet1"::: - :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Microsoft.Win32.SafeHandles.SafeWaitHandle/vb/sample.vb" id="Snippet1"::: - + :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Microsoft.Win32.SafeHandles.SafeWaitHandle/vb/sample.vb" id="Snippet1"::: + ]]> @@ -169,14 +169,14 @@ to reliably release the handle during the finalization phase; to prevent reliable release (not recommended). Initializes a new instance of the class. - class and the unmanaged `CreateMutex` function. - + class and the unmanaged `CreateMutex` function. + :::code language="csharp" source="~/snippets/csharp/Microsoft.Win32.SafeHandles/SafeWaitHandle/.ctor/sample.cs" id="Snippet1"::: - :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Microsoft.Win32.SafeHandles.SafeWaitHandle-ctor/vb/sample.vb" id="Snippet1"::: - + :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Microsoft.Win32.SafeHandles.SafeWaitHandle-ctor/vb/sample.vb" id="Snippet1"::: + ]]>