为什么 Visual Studio 会跳过 ThreadPool 工作项中的异常?
我知道另一篇帖子,但它没有似乎适用于我的情况。
首先,这是我的环境:
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);
}
这是我一直在做的事情:
- 我在 里面的
EnsureTcpSocket
行SetTcpChannel
、 - F11(步入)
EnsureTcpSocket
所以我可以看到_tcp
部分 代码 - 尝试按 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:
- I set a breakpoint at the
EnsureTcpSocket
line insideSetTcpChannel
, - F11 (step-into)
EnsureTcpSocket
so I can see the_tcp
piece of
code - 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你等了多久? 根据参数的具体内容,
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.)您的 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.