gpt4 book ai didi

winforms - visual studio ide 按下暂停按钮停止在运行而不是我想要的地方?

转载 作者:行者123 更新时间:2023-12-05 08:44:02 25 4
gpt4 key购买 nike

困扰我很长时间的事情我终于决定在 stackoverflow 上问一下:

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

 Application.Run(new FormGuiV2()); // pressing pause stops here

这对我来说似乎是一个缺陷。为什么它不在实际执行的代码行上暂停?停在栈顶对调试没有帮助。我想我一定是错误地使用了调试器?

为了绕过它,我必须知道正在执行哪些代码行并放置一个调试点,这很容易出错,有时还需要花费时间来追踪我想要放置调试点的位置。

我想知道单击暂停按钮并使其真正停止在执行代码行处的正确方法。

谢谢

最佳答案

它不会,当您发出 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]

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

请注意堆栈跟踪中的 > 标记。那就是您正在谈论的代码行。您看到它的原因是因为它上面的代码是 .NET 框架的一部分。如果您没有安装 Reference Source,您就没有它的源代码。

所以调试器只是向下遍历堆栈,寻找任何相关的源代码来显示。并且不可避免地会在 Program.cs 中的 Main() 方法中结束,即 Application.Run() 调用。

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

关于winforms - visual studio ide 按下暂停按钮停止在运行而不是我想要的地方?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17624522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com