Skip to content

Commit 1975883

Browse files
authored
Fix compilation error and modernize (#10334)
1 parent 967ba24 commit 1975883

File tree

3 files changed

+45
-58
lines changed

3 files changed

+45
-58
lines changed
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<OutputType>Library</OutputType>
5-
<TargetFramework>net6.0</TargetFramework>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
67
</PropertyGroup>
78

89
</Project>

snippets/csharp/Microsoft.Win32.SafeHandles/SafeWaitHandle/Overview/sample.cs

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class SafeHandlesExample
77
{
88
static void Main()
99
{
10-
UnmanagedMutex uMutex = new UnmanagedMutex("YourCompanyName_SafeHandlesExample_MUTEX");
10+
UnmanagedMutex uMutex = new("YourCompanyName_SafeHandlesExample_MUTEX");
1111

1212
try
1313
{
@@ -22,49 +22,43 @@ static void Main()
2222
finally
2323
{
2424
uMutex.Release();
25-
Console.WriteLine("Mutex Released.");
25+
Console.WriteLine("Mutex released.");
2626
}
27-
28-
Console.ReadLine();
2927
}
3028
}
3129

32-
class UnmanagedMutex
30+
partial class UnmanagedMutex(string Name)
3331
{
34-
3532
// Use interop to call the CreateMutex function.
36-
// For more information about CreateMutex,
37-
// see the unmanaged MSDN reference library.
38-
[DllImport("kernel32.dll", CharSet=CharSet.Unicode)]
39-
static extern SafeWaitHandle CreateMutex(IntPtr lpMutexAttributes, bool bInitialOwner,
40-
string lpName);
33+
[LibraryImport("kernel32.dll", EntryPoint = "CreateMutexW", StringMarshalling = StringMarshalling.Utf16)]
34+
private static partial SafeWaitHandle CreateMutex(
35+
IntPtr lpMutexAttributes,
36+
[MarshalAs(UnmanagedType.Bool)] bool bInitialOwner,
37+
string lpName
38+
);
4139

4240
// Use interop to call the ReleaseMutex function.
4341
// For more information about ReleaseMutex,
4442
// see the unmanaged MSDN reference library.
45-
[DllImport("kernel32.dll")]
46-
public static extern bool ReleaseMutex(SafeWaitHandle hMutex);
43+
[LibraryImport("kernel32.dll")]
44+
[return: MarshalAs(UnmanagedType.Bool)]
45+
public static partial bool ReleaseMutex(SafeWaitHandle hMutex);
4746

48-
private SafeWaitHandle handleValue = null;
49-
private IntPtr mutexAttrValue = IntPtr.Zero;
50-
private string nameValue = null;
51-
52-
public UnmanagedMutex(string Name)
53-
{
54-
nameValue = Name;
55-
}
47+
private SafeWaitHandle _handleValue = null;
48+
private readonly IntPtr _mutexAttrValue = IntPtr.Zero;
49+
private string nameValue = Name;
5650

5751
public void Create()
5852
{
5953
ArgumentException.ThrowIfNullOrEmpty(nameValue);
6054

61-
handleValue = CreateMutex(mutexAttrValue,
55+
_handleValue = CreateMutex(_mutexAttrValue,
6256
true, nameValue);
6357

6458
// If the handle is invalid,
6559
// get the last Win32 error
6660
// and throw a Win32Exception.
67-
if (handleValue.IsInvalid)
61+
if (_handleValue.IsInvalid)
6862
{
6963
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
7064
}
@@ -74,11 +68,9 @@ public SafeWaitHandle Handle
7468
{
7569
get
7670
{
77-
// If the handle is valid,
78-
// return it.
79-
if (!handleValue.IsInvalid)
71+
if (!_handleValue.IsInvalid)
8072
{
81-
return handleValue;
73+
return _handleValue;
8274
}
8375
else
8476
{
@@ -87,17 +79,11 @@ public SafeWaitHandle Handle
8779
}
8880
}
8981

90-
public string Name
91-
{
92-
get
93-
{
94-
return nameValue;
95-
}
96-
}
82+
public string Name => nameValue;
9783

9884
public void Release()
9985
{
100-
ReleaseMutex(handleValue);
86+
ReleaseMutex(_handleValue);
10187
}
10288
}
10389
//</Snippet1>

xml/Microsoft.Win32.SafeHandles/SafeWaitHandle.xml

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,22 @@
6565
<Docs>
6666
<summary>Represents a wrapper class for a wait handle.</summary>
6767
<remarks>
68-
<format type="text/markdown"><![CDATA[
69-
70-
## Remarks
71-
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.
72-
68+
<format type="text/markdown"><![CDATA[
69+
70+
## Remarks
71+
72+
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.
73+
7374
> [!IMPORTANT]
74-
> 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.
75-
76-
77-
78-
## Examples
79-
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.
80-
75+
> 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).
76+
77+
## Examples
78+
79+
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.
80+
8181
:::code language="csharp" source="~/snippets/csharp/Microsoft.Win32.SafeHandles/SafeWaitHandle/Overview/sample.cs" id="Snippet1":::
82-
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Microsoft.Win32.SafeHandles.SafeWaitHandle/vb/sample.vb" id="Snippet1":::
83-
82+
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Microsoft.Win32.SafeHandles.SafeWaitHandle/vb/sample.vb" id="Snippet1":::
83+
8484
]]></format>
8585
</remarks>
8686
<altmember cref="T:Microsoft.Win32.SafeHandles.SafeHandleZeroOrMinusOneIsInvalid" />
@@ -169,14 +169,14 @@
169169
<see langword="true" /> to reliably release the handle during the finalization phase; <see langword="false" /> to prevent reliable release (not recommended).</param>
170170
<summary>Initializes a new instance of the <see cref="T:Microsoft.Win32.SafeHandles.SafeWaitHandle" /> class.</summary>
171171
<remarks>
172-
<format type="text/markdown"><![CDATA[
173-
174-
## Examples
175-
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.
176-
172+
<format type="text/markdown"><![CDATA[
173+
174+
## Examples
175+
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.
176+
177177
:::code language="csharp" source="~/snippets/csharp/Microsoft.Win32.SafeHandles/SafeWaitHandle/.ctor/sample.cs" id="Snippet1":::
178-
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Microsoft.Win32.SafeHandles.SafeWaitHandle-ctor/vb/sample.vb" id="Snippet1":::
179-
178+
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Microsoft.Win32.SafeHandles.SafeWaitHandle-ctor/vb/sample.vb" id="Snippet1":::
179+
180180
]]></format>
181181
</remarks>
182182
<altmember cref="T:Microsoft.Win32.SafeHandles.SafeHandleZeroOrMinusOneIsInvalid" />

0 commit comments

Comments
 (0)