debuggingwindows-services.net-6.0visual-studio-2022topshelf

VS 2022 debugger steps twice on the same line of C# code - windows service using TopShelf


I have a .Net 6 project built using TopShelf in C#, running as windows service.

I want to debug my code to a certain line. I then press F10 but it steps into the line and then out on the 2nd F10 press. So in other words, it like breaks on the same line twice.

Why is that?

My debugger settings are set as default.

Here is an illustration of what I mean:

animated gif showing debugger stepping over same line twice

Any idea why it's doing that?


Solution

  • By the looks of it, its two different threads hitting the same line of code, and you are getting switched between them. Since you are using Topshelf, I'm not surprised something like this happens. I am assuming this service you are debugging is multi-threaded.

    You can tell the thread changed because the yellow color of the highlighted line of code changes from a bright yellow to a more dim one. bCopy.WriteToServer(dt) must not be thread safe, and that causes the exception (with the second thread by the looks of it since the catch line is highlighted in the dimmer yellow color). You can also tell if you changed threads by looking at the thread number in the top tool bar of Visual Studio when this happens. The exception message also says its being used by "another process".