[英]Application hangs only when breakpoint is triggered, .NET, C#, Visual Studio
I refactored my application a while ago and since then I've been having problems with debugging using Visual Studio 2010. 我刚刚重构了我的应用程序,从那以后我一直在使用Visual Studio 2010进行调试时出现问题。
My application works as expected while not debugging (not stepping through the application. An attached debugger does not cause any issues). 我的应用程序按预期工作,而不是调试(不通过应用程序。附加的调试器不会导致任何问题)。 However, when a breakpoint is triggered and I start to step through the app, Visual Studio and the app both hang after at most 3-4 steps.
但是,当一个断点被触发并且我开始逐步完成应用程序时,Visual Studio和应用程序都会在最多3-4步之后挂起。
To emphasize this point even more: It works well with my customers and regardless of whether I start it from Visual Studio or stand-alone - as long as no break point is triggered. 为了更加强调这一点:只要没有触发断点,它就能很好地与我的客户合作,无论我是从Visual Studio还是独立开始。
It does not matter where in the code I place the break point. 在代码中我放置断点并不重要。
IDE: Visual Studio 2010 x64 IDE:Visual Studio 2010 x64
Platform: .NET 4.0 平台:.NET 4.0
The refactoring included a lot of cross-thread calls to BeginInvoke
- all channeled through the following method: 重构包括很多对
BeginInvoke
的跨线程调用 - 所有这些都通过以下方法引导:
public static void BeginInvokeIfRequired(this Control control, Action action)
{
if (control.InvokeRequired)
{
control.BeginInvoke(action);
}
else
{
action.Invoke();
}
}
There is not a single call to Control.Invoke()
in the project. 项目中没有一次调用
Control.Invoke()
。
Additionally, I'd appreciate any hints on how you would track down this bug. 另外,我很欣赏你如何追踪这个bug的任何提示。 My current approach is to add output to the console and selectively deactivating parts of the code.
我目前的方法是将输出添加到控制台并有选择地停用部分代码。
I would suspect that in some cases the code you show poses a problem since InvokeRequired
lies in case IsHandleCreated
is false
- it returns false even if you are not on the GUI thread. 我怀疑在某些情况下,您显示的代码会产生问题,因为
InvokeRequired
位于IsHandleCreated
为false
情况下 - 即使您不在GUI线程上,它也会返回false。
For reference see http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invokerequired.aspx . 有关参考,请参阅http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invokerequired.aspx 。
The following code throws an exception instead of hanging... the fact that it "works as expected" when no breakpoint is hit might be a result of the debugger freezing all threads on hitting a breakpoint which in turn might lead to a different order of execution etc. 以下代码抛出异常而不是挂起...当没有命中断点时它“按预期工作”的事实可能是调试器在命中断点时冻结所有线程的结果,这反过来可能导致不同的顺序执行等
Altogether this means: you might have some "race condition" in your code where BeginInvokeIfRequired
is called on a freshly created control before that control has a Handle
. 总而言之,这意味着:您可能在代码中有一些“竞争条件”,在该控件具有
Handle
之前,在新创建的控件上调用BeginInvokeIfRequired
。 This can even be some 3rd-party code you use... 这甚至可以是你使用的一些第三方代码......
public static void BeginInvokeIfRequired(this Control control, Action action)
{
if (control.IsHandleCreated)
{
if (control.InvokeRequired)
{
control.BeginInvoke(action);
}
else
{
action.Invoke();
}
}
else {
// in this case InvokeRequired might lie !
throw new Exception ( "InvokeRequired is possibly wrong in this case" );
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.