How can I tell which process I am debugging (attached to multiple processes) in Visual Studio?

5.8k Views Asked by At

I'm debugging a Windows service which has two running instances, by attaching to both instances. I am doing this because I know only one instance will hit my breakpoint, but I'd like to know which instance that is, so that I don't have to attach to both in future.

Is there a way, when attached to multiple processes, that you can tell which one has hit a breakpoint? A trial-and-error solution would be to attach one at a time and see if the breakpoint is hit, or, stop one of the services (through Services.msc) and see which process ID disappears, but neither solution seems scale-able to me. Is there a more elegant way?

3

There are 3 best solutions below

2
On BEST ANSWER

You could add a watch to the following statement:

System.Diagnostics.Process.GetCurrentProcess().Id

This gives you the PID (process id) you are attached to. You can look for pids in windows task manager (Menu View->Select Columns and tick PID).

Hope this helps

1
On

You can try the "When Hit..." option available on a breakpoint (right click on the breakpoint, it's in the context menu that pops up). You can then print a message with the value of a variable along with lots of other information, such as:

$ADDRESS - Current Instruction

$CALLER - Previous Function Name

$CALLSTACK - Call Stack

$FUNCTION - Current Function Name

$PID - Process ID

$PNAME - Process Name

$TID - Thread ID

$TNAME - Thread Name

http://msdn.microsoft.com/en-us/library/232dxah7(v=vs.110).aspx

1
On

That's what the Processes window is for (Debug->Windows->Processes, or Ctrl+Alt+Z).