To reproduce: create any .NET 7 console project with Visual Studio 2022. Put a breakpoint at the line on the closing bracket. Start debugging, you'll observe the breakpoint being hit. I expect a press of Continue(F5) will run through this line but actually, it will be hit again.
public static class MainClass
{
public static void Main()
{
DoTask();
}
static void DoTask()
{
using ManualResetEvent mre = new(initialState: true);
mre.WaitOne(500);
} // Hits twice
}
This will not happen if the method is async.
public static class MainClass
{
public static async Task Main()
{
await DoTaskAsync();
}
static async Task DoTaskAsync()
{
using ManualResetEvent mre = new(initialState: true);
mre.WaitOne(500);
await Task.CompletedTask;
} // Won't hit twice
}
I wonder using the "using declaration" with ManualResetEvent has any adverse implications? Hitting the break point twice is not a good sign to me.
- Code edited to not use Top Level Statements