如何在Visual Studio加载项中等待构建命令结束?

时间:2014-05-02 20:41:15

标签: visual-studio visual-studio-2012 visual-studio-addins

我想创建一个Visual Studio加载项,用于构建时间。 到目前为止,这是我的代码:

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

不幸的是,它没有按我的意愿行事,因为_applicationObject.ExecuteCommand会立即返回。

是否可以等待命令的完成或更好,但是当命令结束时会触发一个事件吗?

或者,也许有一种不同的方式来运行构建命令,允许等待或在构建结束时得到通知。

1 个答案:

答案 0 :(得分:0)

我可能太急于创造这个问题了。 Automating Visual Studio instance from separate process包含答案。我应该使用_applicationObject.Solution.SolutionBuild.Build(true);代替_applicationObject.ExecuteCommand("Build.BuildSolution");

就是这样。