Start Debugger in Code

Most simple

To force a breakpoint from code use:

if (System.Diagnostics.Debugger.IsAttached)
    System.Diagnostics.Debugger.Break();

When application wasn't started inside Visual Studio (including remote debugging)

Sometimes the application can't be started from Visual Studio but must be debugged. I use this code to check form inside the application if the Visual Studio is running and offer the possibility to attach it to the Visual Studio.

using System.Diagnostics;

....

// get debugger processes
Process[] procName1 = Process.GetProcessesByName("devenv");

// get remote debugging processes
Process[] procName2 = Process.GetProcessesByName("msvsmon"); 

// If Visual Studio or remote debug are running halt the application by showing a MessageBox and give opportunity to attach the debugger
if (procName1.Length > 0 || procName2.Length > 0)
{
    if (MessageBox.Show(Application.Current.MainWindow, "Force breakpoint?", "Wait for debugger attach", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
    {
        // Force a breakpoint when the debugger became attached
        if (System.Diagnostics.Debugger.IsAttached)
            System.Diagnostics.Debugger.Break(); // force a breakpoint
    }
}

Today I needed a solution for a console app. Here it is:

using System.Diagnostics;

....

// Wait for debugger attach and force breakpoint
// for a console app
//
if (!System.Diagnostics.Debugger.IsAttached) // not needed when debugger is already attached
{
    /// for "eternal" wait (~ 68 years) use: 
    ///     int waitTimeout = int.MaxValue 
    int waitTimeout = 60; 

    // get debugger processes
    Process[] procName1 = Process.GetProcessesByName("devenv");

    // get remote debugging processes
    Process[] procName2 = Process.GetProcessesByName("msvsmon");

    // If Visual Studio or remote debug are running halt the application by showing an message and give opportunity to attach the debugger
    if (procName1.Length > 0 || procName2.Length > 0)
    {
        while (true)
        {
            Console.WriteLine("Visual studio is running | Force breakpoint? (Attach the debugger before pressing any key!)");
            Console.WriteLine("[Y]es, [No]");

            DateTime beginWait = DateTime.Now;
            while (!Console.KeyAvailable && DateTime.Now.Subtract(beginWait).TotalSeconds < waitTimeout)
            {
                Thread.Sleep(250); // sleep 1/4 second
            }

            if (!Console.KeyAvailable)
            {
                break; // timeout elapsed without any kepress
                //<----------
            }

            var key = Console.ReadKey(false);
            if (key.Key == ConsoleKey.Y)
            {
                if (System.Diagnostics.Debugger.IsAttached)
                {
                    System.Diagnostics.Debugger.Break(); // force a breakpoint
                    break; // leave the while(true)
                    //<----------
                }
                else
                {
                    Console.WriteLine("No debugger attached");
                    Console.WriteLine("");
                }
            }
            else if (key.Key == ConsoleKey.N)
            {
                break; // leave the while(true)
                //<----------
            }
            Console.WriteLine("");
        }
    }
}

System.Diagnostics.Debugger.Launch();