Sub AttachProcess()
Dim process As EnvDTE.Process
If Not (DTE.Debugger.DebuggedProcesses Is Nothing) Then
For Each process In DTE.Debugger.DebuggedProcesses
If (process.Name.IndexOf("aspnet_wp.exe") <> -1) Then
Exit Sub
End If
Next
End If
For Each process In DTE.Debugger.LocalProcesses
If (process.Name.IndexOf("myprocess.exe") <> -1) Then
process.Attach()
Exit Sub
End If
Next
End Sub
private AttachResult PessimisticAttach(AttachType attachType)
{
AttachResult res = Attach(attachType);
DateTime timeout = DateTime.Now.AddSeconds(WaitTimeout);
while(res == AttachResult.NotRunning && timeout > DateTime.Now)
{
res = Attach(attachType);
System.Threading.Thread.Sleep(100);
}
return res;
}
private AttachResult Attach(AttachType attachType)
{
string engine =attachTypesMap[attachType];
if(IsBeingDebugged())
{
return AttachResult.BeingDebugged;
}
Debugger2 dbg = dte.Debugger as Debugger2;
Transport trans = dbg.Transports.Item("Default");
Engine eng;
eng = trans.Engines.Item(engine);
EnvDTE80.Process2 proc = null;
try
{
proc = dbg.GetProcesses(trans, "").Item(processName)
as EnvDTE80.Process2;
}
catch(Exception ex)
{
if(ex.Message.Contains("Invalid index."))
{
return AttachResult.NotRunning;
}
}
proc.Attach2(eng);
return AttachResult.Attached;
}
You can download add in source here.