Reputation: 11439
I'm trying to attach to a second process when the debugger starts using the code:
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();
}
It works fine when the debugger is not running, but when trying to attach when trying to attach during the VS_DTE.Events.DebuggerEvents.OnEnterRunMode
event, I get the error:
A macro called a debugger action which is not allowed while responding to an event or while being run because a breakpoint was hit.
How do I attach to another process right when the debugger starts?
Upvotes: 2
Views: 514
Reputation: 11439
I did find an answer to this, it's a hacky solution methinks, but if someone comes along with a better answer I'd love to hear it. In essence you attach the debugger before the debugger actually starts running. Consider:
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();
}
}
}
Upvotes: 1