Search code examples
c++visual-studiofocuscontinue

How to breakall and continue on focus lost in Visual Studio 2017


I got the Edit and continue function setup for my C++ project so i can execute a breakall, change code and then continue to view the results of my code. However it would be better if i didn't have to manually click the buttons. My first solution was using 1 keypress that executes a BreakAll and then the Continue function. However i didn't find a answer for using only Visual studio 2017.

Then i came to a better idea. What if the compiled exe loses its focus and then gains it focus back to make it Execute a BreakAll and then Continue. Or an alternative way which does the same but would be easier to make. Is when Visual Studio loses its focus (because i focused on the compiled exe). It quickly executes a BreakAll and then continues. That would let me write code and execute it in realtime.

My question is does something like that already exists? I couldn't find an answer. And if not does someone have an idea if this is possible to make an a extension? Or is someone willing to make it? Thanks for reading.

EDIT: Here is some Pseudo code.

if compiled_build_running == true && lost_focus == true {
  Debug.BreakAll
  Debug.Continue
}

EDIT2: Alright got it to work with some help and example scripts. It now saves and recompiles when visual studio loses its focus. And BreaksAll when it gains focus. Here is the modified code:

using EnvDTE;
using EnvDTE80;

public class E : VisualCommanderExt.IExtension
{
    public void SetSite(EnvDTE80.DTE2 DTE_, Microsoft.VisualStudio.Shell.Package package) {
    DTE = DTE_;
    System.Windows.Application.Current.Deactivated += OnDeactivated;
    System.Windows.Application.Current.Activated += OnActivated;
}

public void Close() {
    System.Windows.Application.Current.Deactivated -= OnDeactivated;
    System.Windows.Application.Current.Activated -= OnActivated;
}

private void OnDeactivated(object sender, System.EventArgs e) {
    try {
        DTE.ExecuteCommand("File.SaveAll");
    } catch (System.Exception ex) {
    }
    if (DTE.Mode == vsIDEMode.vsIDEModeDebug) {
        DTE.ExecuteCommand("Debug.Start");
    }
}

private void OnActivated(object sender, System.EventArgs e) {

    if (DTE.Mode == vsIDEMode.vsIDEModeDebug) {
        DTE.ExecuteCommand("Debug.BreakAll");
    }

}

private EnvDTE80.DTE2 DTE;
}

Solution

  • You should be able to automate this task using my Visual Commander extension.

    On how to catch the Visual Studio lost focus event see https://stackoverflow.com/a/33592923/84507.

    To check that Visual Studio is currently debugging use:

    if (DTE.Mode == vsIDEMode.vsIDEModeDebug)
    

    To run built-in Visual Studio commands use:

    DTE.ExecuteCommand("Debug.BreakAll");