2

I want to perform action on button click that debugging should be stopped when the button is clicked:

private void button3_Click(object sender, EventArgs e)
{
    //write code here to stop debugging
}
user247702
  • 23,641
  • 15
  • 110
  • 157
AHMAD SUMRAIZ
  • 535
  • 8
  • 21
  • 2
    Hi welcome to SO, if you want to be taken seriously please take some time to revise the grammar in your post. – Mikhail Jan 09 '14 at 08:35
  • Possible duplicate of http://stackoverflow.com/questions/7480518/programmatically-detach-debugger – SOReader Jan 09 '14 at 08:43
  • Can you clarify whether you mean break or detach? Do you mean programatically or are you just talking about a breakpoint? I wonder if we're making this question harder than it is... – Liath Jan 09 '14 at 08:44
  • 1
    Why on earth would you want to do this? – Moo-Juice Jan 09 '14 at 08:45
  • should i go in space :P – AHMAD SUMRAIZ Jan 09 '14 at 10:37
  • Mikhail you was supposed to give answer of problem rather than making suggestions to overcome grammatical mistakes :) – AHMAD SUMRAIZ Jan 09 '14 at 10:39
  • @AHMADSUMRAIZ: Stackoverflow is supposed to have high quality content and there are reviewers for the quality of a post. I agree: grammer matters if the question becomes unclear by bad grammer. – Thomas Weller Jan 09 '14 at 14:03
  • 1
    @AHMADSUMRAIZ Mikhail may have been a bit curt with his comment but he makes a valid point and you shouldn't take offence. In the end in benefits you to have a clearly phrased question as it makes providing an answer that much easier. – Amicable Jan 09 '14 at 14:05
  • @Amicable I think question is in easy language and every one is making comments about grammar and i am not here to ask you about grammar . if you know the answer write it otherwise leave me with my grammar :) – AHMAD SUMRAIZ Jan 10 '14 at 05:30

2 Answers2

1

How do you mean 'stop debugging' you can use the Detach option in Visual Studio, but that isn't in code. To exit the application while debugging use this (windows forms code):

if (Debugger.IsAttached)
{
    Application.Exit();
}
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
1

If you are after programmatically detaching debugger, you need to get a reference to the currently running EnvDTE80.DTE2 object. Once you have that, you could try:

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

To get a reference to EnvDTE80.DTE2, adabyron's approach seems to work: Get the reference of the DTE2 object in Visual C# 2010

You can wrap it all in some class like so:

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;
    }
}

Then, using your event handler:

private void button3_Click(object sender, EventArgs e)
{
    //write code here to stop debugging
    DetachDebugger.Detach();
}
Community
  • 1
  • 1
Matej
  • 21
  • 2