我试图在我的( native C++)应用程序具有鼠标焦点时中断,并且通过将 Visual Studio 应用程序切换到 Ctrl-Alt-Break 或调试 > 全部中断,我的应用程序不再执行我正在尝试的操作继续前进。
例如,有没有办法让 Visual Studio 在 5 秒后中断?或者是否有某种调试 api,我可以编写脚本来触发 Break All 而无需将焦点切换到 Visual Studio 应用程序?
能够在没有焦点的情况下中断可以帮助 poor man's profiling并更好地理解与焦点相关的不熟悉的代码。
最佳答案
有两种方法可以在不改变鼠标焦点到 Visual Studio 的情况下中断:
添加一个带有计时器的线程并在那里设置一个断点,例如将此添加到某处以每 5 秒中断一次:
static bool debug_timer_started = false; if ( !debug_timer_started ) { debug_timer_started = true; std::thread([] { static int seconds = 5; static bool keep_going = true; while ( keep_going ) { /* set break point on this line: */ std::this_thread::sleep_for(std::chrono::seconds(seconds)); } }).detach(); }
您将需要这些 header :
#include <thread> #include <chrono>
关于c++ - 如何让 Visual Studio 调试器在没有焦点的情况下中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36897854/