wpf 如何在 Socket 上调试这个第一次机会异常?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21006302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:27:42  来源:igfitidea点击:

How do I debug this first chance exception on a Socket?

c#.netwpfsockets

提问by user316117

In my C#/WPF/.Net 4.5 program I'm trying to do a Disconnect()on a socket and when I do I get a

在我的 C#/WPF/.Net 4.5 程序中,我试图在套接字上执行Disconnect(),当我这样做时,我得到一个

A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll

“System.Net.Sockets.SocketException”类型的第一次机会异常发生在 System.dll 中

. . . in the Visual Studio Output window.

. . . 在 Visual Studio 输出窗口中。

My code is wrapped in a try/catch but it never lands in the catch . . .

我的代码包含在 try/catch 中,但它永远不会落在 catch 中。. .

    try
      {
          if (_TCPConn.Connected)
          {
              _TCPConn.Shutdown(SocketShutdown.Both);
              _TCPConn.Disconnect(true);   // SocketException
          }
      }
      catch (Exception e)
      {
   . . . 
      }

Up until this point the Socket works fine - it's used to communicate with some manufacturing machinery and all that stuff works perfectly. I've tried this with and without the Shutdown()with the same results. If I let my program keep running after the first chance exception the whole program just disappears and exits the debugger. Then the message in the Output window just says

到目前为止,Socket 工作正常 - 它用于与一些制造机器进行通信,并且所有这些东西都可以完美运行。我在使用和不使用Shutdown() 的情况下都尝试过,结果相同。如果我让我的程序在第一次机会异常后继续运行,整个程序就会消失并退出调试器。然后输出窗口中的消息只是说

The program '[0xE6C] ProcFacTest.vshost.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).

程序“[0xE6C] ProcFacTest.vshost.exe: Managed (v4.0.30319)”已退出,代码为 0 (0x0)。

How do I figure out what's causing this?

我如何找出导致这种情况的原因?

回答by JaredPar

The first step is to disable "Just My Code". This probably shouldn't be necessary but it's possible the IDE is confusing your user code with system code and suppressing the exception as a result. I generally do this when doing exception debugging

第一步是禁用“仅我的代码”。这可能不是必需的,但 IDE 可能会将您的用户代码与系统代码混淆并因此抑制异常。我通常在做异常调试时这样做

  • Tools -> Options -> Debugging
  • Uncheck "Enable Justy My Code Debugging"
  • 工具 -> 选项 -> 调试
  • 取消选中“启用 Justy 我的代码调试”

The next step is to tell Visual Studio to break when that particular exception is thrown.

下一步是告诉 Visual Studio 在引发特定异常时中断。

  • Debug -> Exceptions
  • Expand "Common Language Runtime Exceptions" until you reach "System.Net.Sockets.SocketException"
  • Check "Thrown" for this exception
  • 调试 -> 异常
  • 展开“公共语言运行时异常”,直到到达“System.Net.Sockets.SocketException”
  • 检查“抛出”这个异常