视觉工作室ide按下暂停按钮停在RUN而不是我想要的地方?

sap*_*ket 1 debugging visual-studio-2010 winforms

一段时间以来一直困扰着我的东西我终于决定在stackoverflow上问这里了:

为什么在DEBUG模式下执行Windows窗体项目时,当我单击暂停按钮时,它将始终在Application.Run上停止?

 Application.Run(new FormGuiV2()); // pressing pause stops here
Run Code Online (Sandbox Code Playgroud)

这对我来说似乎是一个缺陷.为什么它不会暂停在正在执行的代码行上?调试时,在堆栈顶部停止是没有用的.我想我必须错误地使用调试器?

为了解决这个问题,我必须知道正在执行哪些代码行并放置一个调试点,这很容易出错,有时需要耗费时间来追踪我想放置调试点的位置.

我想知道点击暂停按钮的正确方法,并让它实际停在执行的代码行.

谢谢

Han*_*ant 6

它没有,当你给出Debug + Break命令时,它停止在任何活动的代码.您可以在调试器的"调用堆栈"窗口中看到该内容.然而,你打断任何你自己的执行代码的可能性非常小,你的程序花99%的时间等待Windows告诉它发生了一些有趣的事情.这使得调用堆栈通常如下所示:

    [Managed to Native Transition]  
    System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(System.IntPtr dwComponentID, int reason, int pvLoopData) + 0x444 bytes    
    System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int reason, System.Windows.Forms.ApplicationContext context) + 0x155 bytes  
    System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int reason, System.Windows.Forms.ApplicationContext context) + 0x4a bytes    
    System.Windows.Forms.dll!System.Windows.Forms.Application.Run(System.Windows.Forms.Form mainForm) + 0x31 bytes  
>   WindowsFormsApplication1.exe!WindowsFormsApplication1.Program.Main() Line 16 + 0x1d bytes   C#
    [Native to Managed Transition]  
    [Managed to Native Transition]  
    mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile, System.Security.Policy.Evidence assemblySecurity, string[] args) + 0x6b bytes    
    Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() + 0x27 bytes  
    mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state) + 0x6f bytes   
    mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) + 0xa7 bytes  
    mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) + 0x16 bytes  
    mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x41 bytes    
    mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x44 bytes   
    [Native to Managed Transition]  
Run Code Online (Sandbox Code Playgroud)

请注意FPushMessageLoop()方法如何位于堆栈跟踪之上.这是Windows GUI应用程序中着名的消息循环,即接收Windows通知的应用程序.如果您还启用了非托管调试,那么您将看到更多内容,核心GetMessage()winapi函数是消息循环的重要组成部分.

请记下>堆栈跟踪中的标记.这就是你所谈论的代码行.你看到它的原因是因为它上面的代码是.NET框架的一部分.如果您没有安装参考源,则没有源代码.

因此,调试器只需向下移动堆栈,查找要显示的任何相关源代码.并且不可避免地会在Program.cs中的Main()方法中调用Application.Run().

在GUI应用程序中获取有用的中断需要设置断点.