c++ - 如何让 Visual Studio 调试器在没有焦点的情况下中断

标签 c++ visual-studio debugging

我试图在我的( native C++)应用程序具有鼠标焦点时中断,并且通过将 Visual Studio 应用程序切换到 Ctrl-Alt-Break 或调试 > 全部中断,我的应用程序不再执行我正在尝试的操作继续前进。

例如,有没有办法让 Visual Studio 在 5 秒后中断?或者是否有某种调试 api,我可以编写脚本来触发 Break All 而无需将焦点切换到 Visual Studio 应用程序?

能够在没有焦点的情况下中断可以帮助 poor man's profiling并更好地理解与焦点相关的不熟悉的代码。

最佳答案

有两种方法可以在不改变鼠标焦点到 Visual Studio 的情况下中断:

  1. 安装 Remote Tools and debug from another machine ,

  2. 添加一个带有计时器的线程并在那里设置一个断点,例如将此添加到某处以每 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/

相关文章:

c++ - 在用户定义类型的容器上使用 std::find c++

visual-studio - 从整个项目/解决方案中删除未使用的命名空间

shell - 如何调试fish脚本?

c++ - 为什么bool变量不能设置为0,除了直接赋0?

c++ - 变量、指针、对象和内存地址 : Why do I get this strange result?

.net - 在构建事件命令行中添加注释的正确方法?

xml - 生成不带 REF 元素的 XSD 文件 - 我只想要一个 "literal"XSD 文件

java - IntelliJ IDEA : How can I create an exception breakpoint that stops on all exceptions *except for* ClassNotFoundException?

oracle - pl/sql dbms_scheduler挂起,如何进一步调试?

c++ - visual studio 无法找到 assert.h