Skip to content

Commit 41b8272

Browse files
Fix issue: AbandandMutexException for .net 6 (dotnet#7556)
* Fix sample code for `Mutex` Moved mutex construction to the main method to ensure that the main thread owns the mutex. https://docs.microsoft.com/en-us/dotnet/api/system.threading.mutex?view=net-6.0#examples
1 parent 88e67fe commit 41b8272

File tree

2 files changed

+11
-2
lines changed
  • samples/snippets/csharp/VS_Snippets_CLR_System/system.Threading.Mutex 1Arg Ctor Example/CS

2 files changed

+11
-2
lines changed

samples/snippets/csharp/VS_Snippets_CLR_System/system.Threading.Mutex 1Arg Ctor Example/CS/class1.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
class Test
66
{
7-
// Create a new Mutex. The creating thread owns the Mutex.
8-
private static Mutex mut = new Mutex(true);
7+
private static Mutex mut;
98
private const int numIterations = 1;
109
private const int numThreads = 3;
1110

1211
static void Main()
1312
{
13+
// Create a new Mutex. The creating thread owns the Mutex.
14+
mut = new Mutex(true);
1415
// Create the threads that will use the protected resource.
1516
for(int i = 0; i < numThreads; i++)
1617
{
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>

0 commit comments

Comments
 (0)