VS 2022调试器在相同的C#代码行上步进两次 – 使用TopShelf的Windows服务

huangapple go评论73阅读模式
英文:

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

问题

我有一个使用C#和TopShelf构建的.NET 6项目,作为Windows服务运行。

我想要调试我的代码到某一行。然后我按F10,但它会进入该行,然后在第二次按F10时会跳出。换句话说,就好像在同一行上断开两次。

为什么会这样?

我的调试器设置设置为默认值。

以下是我所说的情况的示例:

VS 2022调试器在相同的C#代码行上步进两次 – 使用TopShelf的Windows服务

有什么想法为什么会这样做吗?

英文:

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:

VS 2022调试器在相同的C#代码行上步进两次 – 使用TopShelf的Windows服务

Any idea why it's doing that?

答案1

得分: 1

从外表看,这是两个不同的线程同时访问同一行代码,而你被切换在它们之间。由于你正在使用Topshelf,我对发生这种情况不感到惊讶。我假设你正在调试的这个服务是多线程的。

你可以通过观察代码中高亮行的黄色颜色从明亮的黄色变为较暗的黄色来判断线程是否已更改。bCopy.WriteToServer(dt) 必须不是线程安全的,这导致了异常(从外表看似乎是第二个线程,因为catch行的颜色变为较暗的黄色)。当发生这种情况时,你还可以通过查看Visual Studio顶部工具栏中的线程编号来判断是否已更改线程。异常消息还说它正被“另一个进程”使用。

英文:

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

huangapple
  • 本文由 发表于 2023年5月30日 10:53:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76361336.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定