0

I want to create a Visual Studio add-in that times the build. Here is my code so far:

public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
{
  handled = false;
  if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
  {
    if(commandName == "BuildCurrentSolution.Connect.BuildCurrentSolution")
    {
      var window = _applicationObject.Windows.Item(Constants.vsWindowKindOutput);
      var outputWindow = (OutputWindow)window.Object;
      OutputWindowPane outputWindowPane = null;

      for (var i = 1; i <= outputWindow.OutputWindowPanes.Count; i++)
      {
        if (outputWindow.OutputWindowPanes.Item(i).Name.Equals("BuildCurrentSolution", StringComparison.CurrentCultureIgnoreCase))
        {
          outputWindowPane = outputWindow.OutputWindowPanes.Item(i);
          break;
        }
      }

      if (outputWindowPane == null)
      {
        outputWindowPane = outputWindow.OutputWindowPanes.Add("BuildCurrentSolution");
      }

      outputWindowPane.OutputString("The build has started.\n");
      var sw = new Stopwatch();
      sw.Start();
      _applicationObject.ExecuteCommand("Build.BuildSolution");
      sw.Stop();
      outputWindowPane.OutputString(string.Format("The build has ended. Elapsed time: {0} seconds.\n", sw.Elapsed.TotalSeconds));
      handled = true;
    }
  }
}

Unfortunately, it does not do what I want it to, because _applicationObject.ExecuteCommand returns immediately.

Is it possible to wait for the completion of the command or better yet is there an event which is fired when the command is over?

Alternatively, maybe there is a different way to run the build command, the one allowing to wait or be notified when the build is over.

mark
  • 59,016
  • 79
  • 296
  • 580

1 Answers1

0

I was probably too eager to create this question. Automating Visual Studio instance from separate process contains the answer. I should use _applicationObject.Solution.SolutionBuild.Build(true); instead of _applicationObject.ExecuteCommand("Build.BuildSolution");.

That's it.

Community
  • 1
  • 1
mark
  • 59,016
  • 79
  • 296
  • 580