我如何从我的 VSIX 中知道构建之后将进行调试会话? [英] How do I know from my VSIX that a build will be followed by a Debug session?

查看:25
本文介绍了我如何从我的 VSIX 中知道构建之后将进行调试会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的 VSIX 在我的 BuildEvents.OnBuildDone 处理程序中做一些不同的事情,如果由于用户想要启动调试会话而触发了构建.

I'd like my VSIX does something different in my BuildEvents.OnBuildDone handler, if the build has been triggered because the user wants to start a Debug session.

我试过了...

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

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

...but unfortunately at that point m_DTE.Mode is not yet equal to vsIDEMode.vsIDEModeDebug.

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

I could start a one or two seconds timer and then check if (m_DTE.Mode == vsIDEMode.vsIDEModeDebug) but this is not a clean and reliable solution.

我可以通过在 BuildEvents.OnBuildBeginBuildEvents.OnBuildDone 处理程序中以某种方式查询 VSIX API 知道成功构建之后将进行调试会话吗?

Can I know from querying the VSIX API somehow, either in BuildEvents.OnBuildBegin or BuildEvents.OnBuildDone handler, that a successful build will be followed by a Debug session?

推荐答案

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

You can monitor Debug.Start command invocation with EnvDTE.CommandEvents. See the following sample C# extension for Visual Commander:

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

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

On my machine the sequence of events is following:

  1. OnBeforeExecute Debug.Start
  2. 构建开始
  3. OnAfterExecute Debug.Start
  4. OnBuildDone

因此,如果您看到此序列,接下来将是调试会话.

So, if you see this sequence, it will be followed by a Debug session.

完成 Serge 的回答.其实我遵守这个顺序:

To complete the Serge answer. Actually I observe this order:

  1. OnBeforeExecute Debug.Start
  2. OnAfterExecute Debug.Start
  3. 构建开始
  4. OnBuildDone

此外,如果 VisualStudio 估计它在调试前没有要构建的内容,则跳过 OnBuildBegin.

Moreover OnBuildBegin is skipped if VisualStudio estimates it has nothing to build before debugging.

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

OnBuildBegin or (if skipped) OnBuildDone is always executed just after OnAfterExecute Debug.Start (tested on VS2010/2012/2013/2015).

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

Spying others command, I can see two commands Build.SolutionConfigurations (and sometime also one or several Debug.StartupProject) are ran in between before/after execute Debug.Start (I observed this behavior only in in 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. 构建开始
  10. OnBuildDone

因此我们可以推断,当这两个事件之一发生时,一个成功的构建将跟随一个调试会话:

Hence we can infer that a successful build will be followed by a Debug session happens when one of these two events occurs:

  • Build.SolutionConfigurationsDebug.StartupProjects 命令在 Debug.Start 命令之前/之后被触发时.
  • 当最后一次 OnAfterExecute Debug.Start 和当前 OnBuildBeginOnBuildDone 之间的时间少于一秒时.
  • when a Build.SolutionConfigurations or a Debug.StartupProjects command is triggered in between before/after Debug.Start command.
  • when there is less than a second between the last OnAfterExecute Debug.Start and the current OnBuildBegin or OnBuildDone.

附带说明一下,当用户要求在不进行调试的情况下启动时,命令 Debug.StartWithoutDebuggingDebug.Start 的作用相同.因此,我们还可以推断一个成功的构建将跟随一个运行(没有调试)会话

As a side note the command Debug.StartWithoutDebugging plays the same role as Debug.Start when the user asks to start without debugging. Hence we can also infer a successful build will be followed by a run (with no debug) session

这篇关于我如何从我的 VSIX 中知道构建之后将进行调试会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆