views:

359

answers:

3

I have a program that Process.Start()'s another program, and shuts it down after N seconds.

Sometimes I choose to attach a debugger to the started program. In those cases, I don't want the process shut down after N seconds.

I'd like the host program to detect if a debugger is attached or not, so it can choose to not shut it down.

Clarification: I'm not looking to detect if a debugger is attached to my process, I'm looking to detect if a debugger is attached to the process I spawned.

+10  A: 
if(System.Diagnostics.Debugger.IsAttached)
{
    // ...
}
Bryan Watts
Doesn't this tell me if a debugger is attached to the Host process? I'm looking for a way to figure out if the spawned process is being debugged.
Lucas Meijer
I see, sorry for misunderstanding your question. I believe the spawned process is the only one which can query that information. If you set up some form of inter-process communication, that would allow the original process to poll for the debugger state.
Bryan Watts
+5  A: 

You will need to P/Invoke down to CheckRemoteDebuggerPresent. This requires a handle to the target process, which you can get from Process.Handle.

itowlson
A: 

I know this is old, but I was having the same issue and realized if you have a pointer to the EnvDTE, you can check if the process is in Dte.Debugger.DebuggedProcesses:

foreach (EnvDTE.Process p in Dte.Debugger.DebuggedProcesses) {
  if (p.ProcessID == spawnedProcess.Id) {
    // stuff
  }
}

The CheckRemoteDebuggerPresent call only checks if the process is being natively debugged, I believe - it won't work for detecting managed debugging.

Aeka