Launching a debugger from an application

You have a managed application. Let’s suppose a Winforms application in VC#. You want your application to launch the debugger automatically. For this purpose, you added in your program the line System.Diagnostics.Debugger.Launch().

When we run the application outside the debugger, it runs fine, launches the debugger and works normally.

 Now what happens when we run the application inside the debugger? Per MSDN documentation, nothing should happen when this line is executed.

System.Diagnostics.Debugger.Launch()                        

This seems to be quite logical. When a process is already attached with the debugger, how can you attach another one? Similar is the case with the remote debugging.

Now the question may arise, which one gets precedence? Whoever arrives first. Alright now how can we know what is happening in background? Interestingly, if try to attach a debugger to a process which has already invoked a debugger through Debugger.Launch() then we would get the message :

"Unable to attach to the process. A debugger is already attached."

 On the other way, if a debugger is already running a process and encounters Debugger.Launch(), it will silently ignore it.