Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start Debugger in Code

I need to debug an application that is started from a one-click install. (VS 2010, Excel VSTO with Office 7). Based on login credentials supplied to the one-click installer application, the user should see one of two splash pages. This all works fine on my machine, but when deployed, changing from the default to the second splash page results in an error.

For the life of me, I can't figure out how to debug the process from within VS2010. I can attach to the login before entering the credentials, but I can't attach to Excel because it isn't launched until I click the OK button.

So, is there some way to have Excel, or rather, my code call the debugger as it is instantiated so I can figure out why my image resource isn't available in the deployed application?

Thanks.

Randy

like image 884
EoRaptor013 Avatar asked Sep 01 '11 21:09

EoRaptor013


People also ask

How do I start a debugger?

Navigate code in the debugger using step commands To start your app with the debugger attached, press F11 (Debug > Step Into). F11 is the Step Into command and advances the app execution one statement at a time. When you start the app with F11, the debugger breaks on the first statement that gets executed.

How do I run code in debug mode?

Press F5 and hover over the type variable again. Repeat this step until you see a value of I in the type variable. Now, press F11 (Debug > Step Into or the Step Into button in the Debug Toolbar). F11 advances the debugger (and executes code) one statement at a time.

What is start debugging?

Start with debugging, is the option, wherein you can pause the procesing of the code, at a certain break point, and then iterate through each step of the code, usually to check the processing of data.


2 Answers

System.Diagnostics.Debugger.Launch(); 
like image 105
Juan Ayala Avatar answered Sep 21 '22 11:09

Juan Ayala


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("");         }     } } 
like image 20
marsh-wiggle Avatar answered Sep 21 '22 11:09

marsh-wiggle