【问题标题】:How do I attach a second process when the debugger starts?调试器启动时如何附加第二个进程?
【发布时间】:2013-07-16 17:46:30
【问题描述】:

当调试器开始使用代码时,我正在尝试附加到第二个进程:

DTE dte = BuildaPackage.VS_DTE;

EnvDTE.Process localServiceEngineProcess = dte.Debugger.LocalProcesses
    .Cast<EnvDTE.Process>()
    .FirstOrDefault(process => process.Name.Contains("ServiceMonitor"));

if (localServiceEngineProcess != null) {
    localServiceEngineProcess.Attach();
}

调试器未运行时工作正常,但在VS_DTE.Events.DebuggerEvents.OnEnterRunMode 事件期间尝试附加时尝试附加时,出现错误:

A macro called a debugger action which is not allowed while responding to an event or while being run because a breakpoint was hit.

如何在调试器启动时附加到另一个进程?

【问题讨论】:

  • 您想在开始调试解决方案中的应用程序时附加到已经运行的进程(不在您的解决方案中)?询问是因为您已经可以在同一个解决方案中调试多个应用程序 - 使用解决方案/启动应用程序设置。
  • 解决方案中没有多个项目,而是我希望 IDE 插件 能够在用户启动他们的应用程序时附加。我有一个单独的 ServiceMonitor 服务器来托管用户的应用程序,所以我希望 IDE 在用户开始调试时自动附加到该服务器。

标签: c# visual-studio envdte


【解决方案1】:

我确实找到了答案,我认为这是一个 hacky 解决方案,但如果有人提出更好的答案,我很想听听。本质上,您在调试器实际开始运行之前附加调试器。考虑:

internal class DebugEventMonitor {

    // DTE Events are strange in that if you don't hold a class-level reference
    // The event handles get silently garbage collected. Cool!
    private DTEEvents dteEvents;

    public DebugEventMonitor() {
        // Capture the DTEEvents object, then monitor when the 'Mode' Changes.
        dteEvents = DTE.Events.DTEEvents;                     
        this.dteEvents.ModeChanged += dteEvents_ModeChanged;
    }

    void dteEvents_ModeChanged(vsIDEMode LastMode) {
        // Attach to the process when the mode changes (but before the debugger starts).
        if (IntegrationPackage.VS_DTE.DTE.Mode == vsIDEMode.vsIDEModeDebug) {
            AttachToServiceEngineCommand.Attach();
        }
    }

}

【讨论】:

    猜你喜欢
    • 2012-03-21
    • 2023-03-07
    • 2011-05-24
    • 2021-12-02
    • 1970-01-01
    • 1970-01-01
    • 2019-04-30
    • 2014-08-10
    • 1970-01-01
    相关资源
    最近更新 更多