1

我知道this other post,但它似乎不适用于我的情况。

首先,这是我的环境:
Windows XP 64-bit SP3;带有 SP 的 Visual Studio 2008;.NET 3.5 SP1;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. EnsureTcpSocket我在里面的行 设置了一个断点 SetTcpChannel
  2. F11(步入)EnsureTcpSocket 所以我可以看到_tcp这段代码
  3. 尝试 F10 (step-over) TcpClient构造函数

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

有任何想法吗?

4

2 回答 2

2

你等了多久?根据参数的具体内容,TcpClient构造函数可能会等到超时后再抛出异常。(如果连接被拒绝,那就另当别论了。)

于 2009-04-28T18:50:06.647 回答
1

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

或者,在 Visual Studio 中,您可以设置调试选项以中断 First Chance 异常,这是一篇旧文章,描述了如何.

于 2009-04-28T18:51:48.827 回答