代码之家  ›  专栏  ›  技术社区  ›  Erich Mirabal

为什么Visual Studio会跳过线程池工作项中的我的异常?

  •  1
  • Erich Mirabal  · 技术社区  · 16 年前

    post

    首先,这里是我的环境:
    WindowsXP 64位SP3;Visual Studio 2008 w/SP。NET3.5SP1;WPF应用程序。

    我的问题是,我似乎无法找到异常发生的地方。它只是继续运行,而不是继续调试会话。

    // start up the WPF application
    // set the handler for the Checked event
    ToggleButton channelButton1 = new ToggleButton();
    channelButton1.Checked += (s, e) => 
         ThreadPool.QueueUserWorkItem(SetTcpChannel, 1);
    

    那么在那 SetTcpChannel 方法:

        try
        {
            int channel = (int)state;
            EnsureTcpSocket();
            // more logic to do stuff with channel 
            // that we don't care about for SO
            ...
        }
        catch (Exception e)
        {
            // just for illustration
            // this is where I expected the code to return
            ...
        }
    

    EnsureTcpSocket

        if (_tcp == null)
        {
            // this is not a valid ip/port since the 
            // target machine is not running (test condition)
            // but that's ok, I should get some exception 
            // and continue debugging...right?
            _tcp = new TcpClient(ip, port);
        }
    

    以下是我一直在做的事情:

    1. 我设置了一个断点 内线 SETTCP通道 ,
    2. 保险柜 这样我就可以看到 _tcp 密码
    3. TcpClient 建造师

    在最后一步,应用程序从调试器返回并继续运行,我从未看到任何异常。

    有什么想法吗?

    2 回复  |  直到 8 年前
        1
  •  2
  •   Jon Skeet    16 年前

    你等了多久?具体取决于参数是什么 TcpClient 构造函数可能会等到超时后再抛出异常。(如果连接被拒绝,那就不同了。)

        2
  •  1
  •   Sijin    16 年前

    在catch块中有断点吗?如果没有,我不明白为什么调试器会中断执行并停止在那里?

    或者,在VisualStudio中,您可以设置调试选项以在第一次出现异常时中断,如下所示 an old post that describes how .