C # force debug mode

Nevermind, why this might be needed, I'm just curious to know if this can be done

Here is my code that does not work:

if (!Debugger.IsAttached) { try { Debugger.Launch(); while (!Debugger.IsAttached) { Thread.Sleep(1000); } } catch (System.Security.SecurityException e) { Console.WriteLine("exception " + e.Message); } } 

I was mostly curious about how to use the Debugger.Launch() method.

+8
debugging c #
source share
3 answers

Debugger.Launch will launch the debugger or do nothing, it is already attached. I believe this is not a breakpoint. Debugger.Break () will really break.

Calling Debugger.Launch () can do different things depending on the machine, for example, if Visual Studio is installed or not, etc.

See also the related article: How to start the automatic debugger

+15
source share

It will start and connect the debugger to the process. Of course, do not use it in production. I think that possible use may be on the local machine when an error occurs, and you want to automatically start debugging.

+3
source share

I think you do not understand what Debugger.Launch() does. It functions as a hard-coded breakpoint.

When your program enters Debugger.Launch() , the Just-In-Time-Debugging window will appear (provided that Visual Studio is installed on the computer. At this point, your program is stopped - it does not continue to work.

If you select an instance of VS, it will start and be stopped on the line using Debugger.Launch() , as if you hit a breakpoint.

Therefore, there is no reason for a while () loop. You can just call Debugger.Launch() when you want to stop the program to see something.

But the usefulness of Debugger.Launch() questionable. You can use breakpoints much easier, and with breakpoints there is no danger of accidentally leaving it in the finished product.

+3
source share

All Articles