【问题标题】:How can I programmatically stop debugging?如何以编程方式停止调试?
【发布时间】:2014-01-09 08:30:57
【问题描述】:

我想对按钮单击执行操作,当单击按钮时应停止调试:

private void button3_Click(object sender, EventArgs e)
{
    //write code here to stop debugging
}

【问题讨论】:

  • 您好,欢迎来到 SO,如果您想被认真对待,请花点时间修改您帖子中的语法。
  • 你能澄清一下你的意思是中断还是分离?你的意思是编程还是你只是在谈论一个断点?我想知道我们是否让这个问题变得更难......
  • 你到底为什么要这样做?
  • @AHMADSUMRAIZ 米哈伊尔的评论可能有点生硬,但他的观点是正确的,你不应该生气。最后,你有一个明确表达的问题会让你更容易提供答案。

标签: c# debugging


【解决方案1】:

“停止调试”是什么意思,您可以在 Visual Studio 中使用 Detach 选项,但这不在代码中。要在调试时退出应用程序,请使用以下代码(Windows 窗体代码):

if (Debugger.IsAttached)
{
    Application.Exit();
}

【讨论】:

    【解决方案2】:

    如果您在以编程方式分离调试器之后,您需要获取对当前运行的 EnvDTE80.DTE2 对象的引用。一旦你有了它,你可以尝试:

    var dte = ...
    dte.Debugger.DetachAll()
    

    要获得对 EnvDTE80.DTE2 的引用,adabyron 的方法似乎有效: Get the reference of the DTE2 object in Visual C# 2010

    您可以将它全部包装在某个类中,如下所示:

    using System;
    using System.Collections.Generic;
    using System.Runtime.InteropServices;
    using System.Runtime.InteropServices.ComTypes;
    using EnvDTE80;
    
    class DetachDebugger
    {
        [DllImport("ole32.dll")]
        private static extern void CreateBindCtx(int reserved, out IBindCtx ppbc);
    
        [DllImport("ole32.dll")]
        private static extern int GetRunningObjectTable(int reserved, out IRunningObjectTable prot);
    
        public static void Detach()
        {
            var dte = GetCurrent();
            dte.Debugger.DetachAll();
        }
    
        /// <summary>
        /// Gets the current visual studio's solution DTE2
        /// </summary>
        private static DTE2 GetCurrent()
        {
            List<DTE2> dte2s = new List<DTE2>();
    
            IRunningObjectTable rot;
            GetRunningObjectTable(0, out rot);
            IEnumMoniker enumMoniker;
            rot.EnumRunning(out enumMoniker);
            enumMoniker.Reset();
            IntPtr fetched = IntPtr.Zero;
            IMoniker[] moniker = new IMoniker[1];
            while (enumMoniker.Next(1, moniker, fetched) == 0)
            {
                IBindCtx bindCtx;
                CreateBindCtx(0, out bindCtx);
                string displayName;
                moniker[0].GetDisplayName(bindCtx, null, out displayName);
                // add all VisualStudio ROT entries to list
                if (displayName.StartsWith("!VisualStudio"))
                {
                    object comObject;
                    rot.GetObject(moniker[0], out comObject);
                    dte2s.Add((DTE2)comObject);
                }
            }
    
            // get path of the executing assembly (assembly that holds this code) - you may need to adapt that to your setup
            string thisPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
    
            // compare dte solution paths to find best match
            KeyValuePair<DTE2, int> maxMatch = new KeyValuePair<DTE2, int>(null, 0);
            foreach (DTE2 dte2 in dte2s)
            {
                int matching = GetMatchingCharsFromStart(thisPath, dte2.Solution.FullName);
                if (matching > maxMatch.Value)
                    maxMatch = new KeyValuePair<DTE2, int>(dte2, matching);
            }
    
            return (DTE2)maxMatch.Key;
        }
    
        /// <summary>
        /// Gets index of first non-equal char for two strings
        /// Not case sensitive.
        /// </summary>
        private static int GetMatchingCharsFromStart(string a, string b)
        {
            a = (a ?? string.Empty).ToLower();
            b = (b ?? string.Empty).ToLower();
            int matching = 0;
            for (int i = 0; i < Math.Min(a.Length, b.Length); i++)
            {
                if (!char.Equals(a[i], b[i]))
                    break;
    
                matching++;
            }
            return matching;
        }
    }
    

    然后,使用您的事件处理程序:

    private void button3_Click(object sender, EventArgs e)
    {
        //write code here to stop debugging
        DetachDebugger.Detach();
    }
    

    【讨论】:

      猜你喜欢
      • 2011-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-16
      • 1970-01-01
      • 2019-03-16
      相关资源
      最近更新 更多