2

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

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.

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

4

1 回答 1

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();
        }
    }

}
于 2013-07-29T15:11:18.370 回答