1

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?

Timothy G.
  • 6,335
  • 7
  • 30
  • 46
Fandango68
  • 4,461
  • 4
  • 39
  • 74
  • I'm a little bit confused about the illustration you shared. Could you please provide detailed steps what you've done and your expectation result and actual result ? – Dou Xu-MSFT May 31 '23 at 08:37

1 Answers1

1

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".

Timothy G.
  • 6,335
  • 7
  • 30
  • 46
  • Exactly correct. I never had issues using Topshelf until I've updated it to the latest version. So how do I make my code "thread safe"? Any resource? Thanks. You've nailed it. – Fandango68 Jun 01 '23 at 00:55
  • 1
    @Fandango68 It may not be TopShelf itself, I only meant that *because* you are using it, it made me think that this is some sort of service that could have multiple users possibly working with the same _thing_ at the same time (I have _some_ experience using TopShelf for a Window Service that I maintain). Maybe check out this post https://stackoverflow.com/questions/21310757/thread-safety-for-datatable since I think you are working with a `DataTable` that you are trying to write out to a database. – Timothy G. Jun 01 '23 at 01:03