在代码中启动调试器

EoR*_*013 50 c# debugging visual-studio-2010

我需要调试从一键安装启动的应用程序.(VS 2010,Excel VSTO与Office 7).根据提供给一键安装程序应用程序的登录凭据,用户应该看到两个启动页面中的一个.这一切在我的机器上运行正常,但在部署时,从默认页面更改为第二个启动页面会导致错误.

对于我的生活,我无法弄清楚如何从VS2010内调试过程.我可以在输入凭据之前附加到登录名,但是我无法附加到Excel,因为在我单击"确定"按钮之前它不会启动.

那么,有没有办法让Excel,或者更确切地说,我的代码在实例化时调用调试器,所以我可以弄清楚为什么我的图像资源在部署的应用程序中不可用?

谢谢.

兰迪

Jua*_*ala 106

System.Diagnostics.Debugger.Launch();
Run Code Online (Sandbox Code Playgroud)

  • 哦......那么简单!?!:)不知道. (3认同)
  • 和System.Diagnostics.Debugger.Break到当前位置的断点 (3认同)
  • @KFL 我知道这是一篇旧帖子;当它点击 launch() 时,会弹出一个对话框,询问您要调试的内容。 (3认同)

mar*_*gle 9

最简单

要从代码使用强制断点:

if (System.Diagnostics.Debugger.IsAttached)
    System.Diagnostics.Debugger.Break();
Run Code Online (Sandbox Code Playgroud)

当应用程序未在 Visual Studio 中启动时(包括远程调试)

有时应用程序无法从 Visual Studio 启动,但必须进行调试。如果 Visual Studio 正在运行,我会使用此代码检查应用程序内部的表单,并提供将其附加到 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
    }
}
Run Code Online (Sandbox Code Playgroud)

今天我需要一个控制台应用程序的解决方案。这里是:

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("");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)