Start Debugger in Code

前端 未结 4 812
耶瑟儿~
耶瑟儿~ 2020-12-30 18:28

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

相关标签:
4条回答
  • 2020-12-30 18:45

    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
        }
    }
    
    0 讨论(0)
  • 2020-12-30 18:57

    Juan's answer is the best if you have Visual Studio Installed. but if the target machne does not have it you may need to put in some kind of pause (I normally will put a dialog box as the first thing in main that makes it wait for me to attach) then use a remote debugger to attach to it on your machine

    0 讨论(0)
  • 2020-12-30 19:00
    System.Diagnostics.Debugger.Launch();
    
    0 讨论(0)
  • 2020-12-30 19:08

    you could attach to Excel if it was running long enough but seriously I doubt the error is there.

    you could attach to running applications/processes and if symbols are available (debug build) you can really debug, but the application has to live long enough for you to select it for attaching.

    I think, from what you are saying, that what you need is proper exception and error logging, anything like Log4Net or NLog which stores everything (stack trace, exception details...) at every exception, so you would clearly identify what the real issue is.

    0 讨论(0)
提交回复
热议问题