我如何从我的VSIX知道构建将跟随调试会话?

时间:2015-11-23 07:02:12

标签: visual-studio debugging build vsix

我希望我的VSIX在我的BuildEvents.OnBuildDone处理程序中执行不同的操作,如果由于用户想要启动调试会话而触发了构建。

我试过......

private void m_BuildEvents_OnBuildDone(vsBuildScope scope, vsBuildAction action) {
   if (m_DTE.Mode == vsIDEMode.vsIDEModeDebug) {
      //...
   }
}

...但遗憾的是,此时m_DTE.Mode还不等于vsIDEMode.vsIDEModeDebug

我可以启动一到两秒的计时器,然后检查(m_DTE.Mode == vsIDEMode.vsIDEModeDebug)是否但这不是一个干净可靠的解决方案。

我是否可以通过BuildEvents.OnBuildBeginBuildEvents.OnBuildDone处理程序以某种方式查询VSIX API来了解成功构建后是否会进行调试会话?

1 个答案:

答案 0 :(得分:4)

您可以使用 EnvDTE.CommandEvents 监控 Debug.Start 命令调用。请参阅以下Visual Commander的C#扩展示例:

public class E : VisualCommanderExt.IExtension
{
    public void SetSite(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package)
    {
        events = DTE.Events;
        commandEvents = events.get_CommandEvents(null, 0);
        buildEvents = events.BuildEvents;
        commands = DTE.Commands as EnvDTE80.Commands2;

        commandEvents.BeforeExecute += OnBeforeExecute;
        commandEvents.AfterExecute += OnAfterExecute;

        buildEvents.OnBuildDone += OnBuildDone;
        buildEvents.OnBuildBegin += OnBuildBegin;
    }

    public void Close()
    {
        commandEvents.BeforeExecute -= OnBeforeExecute;
        commandEvents.AfterExecute -= OnAfterExecute;

        buildEvents.OnBuildDone -= OnBuildDone;
        buildEvents.OnBuildBegin -= OnBuildBegin;
    }

    private void OnBeforeExecute(string Guid, int ID, object CustomIn, object CustomOut, ref bool CancelDefault)
    {
        string name = GetCommandName(Guid, ID);
        if (name == "Debug.Start")
        {
            System.Windows.MessageBox.Show("OnBeforeExecute Debug.Start");
        }
    }

    private void OnAfterExecute(string Guid, int ID, object CustomIn, object CustomOut)
    {
        string name = GetCommandName(Guid, ID);
        if (name == "Debug.Start")
        {
            System.Windows.MessageBox.Show("OnAfterExecute Debug.Start " + System.DateTime.Now.Second + "." + System.DateTime.Now.Millisecond);
        }
    }

    private void OnBuildDone(EnvDTE.vsBuildScope scope, EnvDTE.vsBuildAction action)
    {
        System.Windows.MessageBox.Show("OnBuildDone " + System.DateTime.Now.Second + "." + System.DateTime.Now.Millisecond);
    }

    private void OnBuildBegin(EnvDTE.vsBuildScope scope, EnvDTE.vsBuildAction action)
    {
        System.Windows.MessageBox.Show("OnBuildBegin " + System.DateTime.Now.Second + "." + System.DateTime.Now.Millisecond);
    }

    // throw()
    private string GetCommandName(string Guid, int ID)
    {
        if (Guid == null)
            return "null";

        string result = "";
        if (commands != null)
        {
            try
            {
                return commands.Item(Guid, ID).Name;
            }
            catch (System.Exception)
            {
            }
        }
        return result;
    }

    private EnvDTE.Events events;
    private EnvDTE.CommandEvents commandEvents;
    private EnvDTE.BuildEvents buildEvents;
    private EnvDTE80.Commands2 commands;
}

在我的机器上,事件的顺序如下:

  1. OnBeforeExecute Debug.Start
  2. OnBuildBegin
  3. OnAfterExecute Debug.Start
  4. OnBuildDone
  5. 因此,如果您看到此序列,则会跟随调试会话。

    完成Serge的回答。实际上我遵守了这个命令:

    1. OnBeforeExecute Debug.Start
    2. OnAfterExecute Debug.Start
    3. OnBuildBegin
    4. OnBuildDone
    5. 此外,如果VisualStudio估计在调试之前无需构建任何内容,则会跳过OnBuildBegin

      OnBuildBegin或(如果跳过)OnBuildDone总是在OnAfterExecute Debug.Start之后执行(在VS2010 / 2012/2013 / 2015上测试)。

      Spying others命令,我可以看到两个命令Build.SolutionConfigurations(有时还有一个或几个Debug.StartupProject)在执行Debug.Start之前/之后运行(我在此仅观察到此行为)在VS2013 / 2015)。

      1. OnBeforeExecute Debug.Start
      2. OnBeforeExecute Build.SolutionConfigurations
      3. OnAfterExecute Build.SolutionConfigurations
      4. OnBeforeExecute Build.SolutionConfigurations
      5. OnAfterExecute Build.SolutionConfigurations
      6. OnBeforeExecute Debug.StartupProjects
      7. OnAfterExecute Debug.StartupProjects
      8. OnAfterExecute Debug.Start
      9. OnBuildBegin
      10. OnBuildDone
      11. 因此,我们可以推断,当发生以下两个事件之一时,成功构建后将发生调试会话

        • Build.SolutionConfigurations命令之前/之后触发Debug.StartupProjectsDebug.Start命令时。
        • 当最后一次OnAfterExecute Debug.Start与当前OnBuildBeginOnBuildDone之间的时间不到一秒时。

        作为旁注,当用户要求在没有调试的情况下启动时,命令Debug.StartWithoutDebugging扮演与Debug.Start相同的角色。因此,我们也可以推断成功构建后将运行(没有调试)会话