简体   繁体   English

应用程序仅在触发断点时挂起,即.NET,C#,Visual Studio

[英]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()

  • Is there something wrong with the above method? 上述方法有问题吗?

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位于IsHandleCreatedfalse情况下 - 即使您不在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.

相关问题 访问成员变量时在Visual Studio中设置断点C# - Set breakpoint in Visual Studio when a member variable is accessed C# 在Visual Studio上启动.NET MVC应用程序时启动ac#控制台应用程序 - Start a c# console application when .NET MVC Application starts on Visual Studio 在Visual Studio(C#)中进行脱胶时,是否可以在断点之后插入一行代码? - When doing degugs in Visual Studio(C#), Is there a way to insert a line of code after breakpoint? 调试C#时遇到Breakpoint时,防止显示Visual Studio窗口 - Prevent showing visual studio window when Breakpoint hits while debugging c# 当 Visual Studio 调试器评估值时,我可以让我的 C# 代码在断点处停止吗? - Can I make my C# code stop at breakpoint when Visual Studio debugger is evaluating the value? C# - Excel 应用程序仅适用于 Visual Studio - C# - Excel Application is only working on Visual Studio 如果在Visual Studio和C#中,我可以在内联中设置断点吗? - Can I set a breakpoint in an inline if in Visual Studio and C#? 在Visual Studio中的C#通用类/方法上设置函数断点? - Set a Function Breakpoint on a C# Generic Class/Method in Visual Studio? 如何在Visual Studio 2013中的C#结构字段上设置断点 - How to set a breakpoint on a C# structure field in Visual Studio 2013 有没有办法在C#/ Visual Studio的断点上睡觉? - Any way to sleep on a breakpoint in C# / Visual Studio?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM