Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# force debug mode programmatically

Tags:

c#

debugging

Nevermind why one might want to do this, I'm just curious to know if it can be done.

Here's my code that doesn't work:

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

Basically I was curious to know how to use the Debugger.Launch() method.

like image 630
user420667 Avatar asked Sep 18 '11 05:09

user420667


2 Answers

Debugger.Launch will launch a debugger or will do nothing is one is already attached. I believe It's not a breakpoint. Debugger.Break() will actually break.

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

See also a related tech article: How to: Launch the Debugger Automatically

like image 176
Simon Mourier Avatar answered Sep 20 '22 20:09

Simon Mourier


It will launch and attaches a debugger to the process. For sure do not use it in production . I think that possible use can be in local machine when error occured and you want automatically run debug.

like image 39
Gregory Nozik Avatar answered Sep 20 '22 20:09

Gregory Nozik