Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net6.0</TargetFramework>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SafeHandlesExample
{
static void Main()
{
UnmanagedMutex uMutex = new UnmanagedMutex("YourCompanyName_SafeHandlesExample_MUTEX");
UnmanagedMutex uMutex = new("YourCompanyName_SafeHandlesExample_MUTEX");

try
{
Expand All @@ -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());
}
Expand All @@ -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
{
Expand All @@ -87,17 +79,11 @@ public SafeWaitHandle Handle
}
}

public string Name
{
get
{
return nameValue;
}
}
public string Name => nameValue;

public void Release()
{
ReleaseMutex(handleValue);
ReleaseMutex(_handleValue);
}
}
//</Snippet1>
42 changes: 21 additions & 21 deletions xml/Microsoft.Win32.SafeHandles/SafeWaitHandle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,22 @@
<Docs>
<summary>Represents a wrapper class for a wait handle.</summary>
<remarks>
<format type="text/markdown"><![CDATA[

## Remarks
The <xref:Microsoft.Win32.SafeHandles.SafeWaitHandle> class is used by the <xref:System.Threading.WaitHandle?displayProperty=nameWithType> class. It is a wrapper for Win32 mutexes and auto and manual reset events.

<format type="text/markdown"><![CDATA[

## Remarks

The <xref:Microsoft.Win32.SafeHandles.SafeWaitHandle> class is used by the <xref:System.Threading.WaitHandle?displayProperty=nameWithType> class. It is a wrapper for Win32 mutexes and auto and manual reset events.

> [!IMPORTANT]
> This type implements the <xref:System.IDisposable> 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 <xref:System.IDisposable.Dispose%2A> 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 <xref:System.IDisposable> interface topic.



## Examples
The following code example demonstrates how to use interop to create a mutex using the <xref:Microsoft.Win32.SafeHandles.SafeWaitHandle> class and the unmanaged `CreateMutex` function.

> This type implements the <xref:System.IDisposable> 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 <xref:System.IDisposable.Dispose%2A> 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 <xref:Microsoft.Win32.SafeHandles.SafeWaitHandle> 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":::

]]></format>
</remarks>
<altmember cref="T:Microsoft.Win32.SafeHandles.SafeHandleZeroOrMinusOneIsInvalid" />
Expand Down Expand Up @@ -169,14 +169,14 @@
<see langword="true" /> to reliably release the handle during the finalization phase; <see langword="false" /> to prevent reliable release (not recommended).</param>
<summary>Initializes a new instance of the <see cref="T:Microsoft.Win32.SafeHandles.SafeWaitHandle" /> class.</summary>
<remarks>
<format type="text/markdown"><![CDATA[
## Examples
The following code example demonstrates how to use interop to create a mutex using the <xref:Microsoft.Win32.SafeHandles.SafeWaitHandle> class and the unmanaged `CreateMutex` function.
<format type="text/markdown"><![CDATA[

## Examples
The following code example demonstrates how to use interop to create a mutex using the <xref:Microsoft.Win32.SafeHandles.SafeWaitHandle> 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":::

]]></format>
</remarks>
<altmember cref="T:Microsoft.Win32.SafeHandles.SafeHandleZeroOrMinusOneIsInvalid" />
Expand Down