为什么 Visual Studio 会跳过 ThreadPool 工作项中的异常?

发布于 2024-07-18 08:06:18 字数 1648 浏览 8 评论 0原文

我知道另一篇帖子,但它没有似乎适用于我的情况。

首先,这是我的环境:
Windows XP 64 位 SP3; Visual Studio 2008 带 SP; .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. 我在 里面的 EnsureTcpSocketSetTcpChannel
  2. F11(步入)EnsureTcpSocket 所以我可以看到 _tcp 部分 代码
  3. 尝试按 F10(步进) TcpClient 构造函数

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

有任何想法吗?

I am aware of this other post, but it doesn't seem to apply to my situation.

First off, here is my environment:
Windows XP 64-bit SP3; Visual Studio 2008 w/ SP; .NET 3.5 SP1; WPF application.

My problem is that I cannot seem to step to where my exception happens. It just keeps running instead of continuing the debugging session.

Here is some code -- I didn't write all of it, so please, no need for comments on naming :)

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

Then in that SetTcpChannel method:

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

And finally, in the place where the exception actually happens (inside 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);
    }

Here is what I have been doing:

  1. I set a breakpoint at the
    EnsureTcpSocket line inside
    SetTcpChannel,
  2. F11 (step-into) EnsureTcpSocket
    so I can see the _tcp piece of
    code
  3. Try to F10 (step-over) the
    TcpClient constructor

At that last step, the application comes back up from the debugger and keeps running and I never see any exception.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我要还你自由 2024-07-25 08:06:18

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

How long did you wait? Depending on exactly what the arguments are, the TcpClient constructor may wait until it times out before throwing the exception. (If the connection is refused, that's a different matter.)

巡山小妖精 2024-07-25 08:06:18

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

或者,在 Visual Studio 中,您可以设置调试选项以在第一次机会异常时中断,如下所示 一篇描述如何操作的旧帖子

Do you have a breakpoint inside the catch block, if not I don't see why the debugger would break the execution and stop there?

Alternatively, in Visual Studio you can set debugging options to break on First Chance exceptions, here's an old post that describes how.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文