我如何从VSIX知道在构建之后会进行Debug会话? [英] How do I know from my VSIX that a build will be followed by a Debug session?
问题描述
如果由于用户想要启动Debug会话而触发了构建,我希望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.OnBuildBegin
或BuildEvents.OnBuildDone
处理程序中查询VSIX API,我可以知道在成功构建之后将有一个Debug会话吗?
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 命令调用.请参见以下示例C#扩展的 Visual Commander
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:
- OnBeforeExecute
Debug.Start
- OnBuildBegin
- OnAfterExecute
Debug.Start
- OnBuildDone
- OnBeforeExecute
Debug.Start
- OnBuildBegin
- OnAfterExecute
Debug.Start
- OnBuildDone
因此,如果您看到此序列,将在其后跟随一个Debug会话.
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:
- OnBeforeExecute
Debug.Start
- OnAfterExecute
Debug.Start
- OnBuildBegin
- OnBuildDone
- OnBeforeExecute
Debug.Start
- OnAfterExecute
Debug.Start
- OnBuildBegin
- 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).
监视其他命令,我可以看到在执行Debug.Start
之前/之后在两个命令Build.SolutionConfigurations
(有时也有一个或几个Debug.StartupProject
)之间运行(我仅在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).
- OnBeforeExecute
Debug.Start
- OnBeforeExecute
Build.SolutionConfigurations
- OnAfterExecute
Build.SolutionConfigurations
- OnBeforeExecute
Build.SolutionConfigurations
- OnAfterExecute
Build.SolutionConfigurations
- OnBeforeExecute
Debug.StartupProjects
- OnAfterExecute
Debug.StartupProjects
- OnAfterExecute
Debug.Start
- OnBuildBegin
- OnBuildDone
- OnBeforeExecute
Debug.Start
- OnBeforeExecute
Build.SolutionConfigurations
- OnAfterExecute
Build.SolutionConfigurations
- OnBeforeExecute
Build.SolutionConfigurations
- OnAfterExecute
Build.SolutionConfigurations
- OnBeforeExecute
Debug.StartupProjects
- OnAfterExecute
Debug.StartupProjects
- OnAfterExecute
Debug.Start
- OnBuildBegin
- OnBuildDone
因此,我们可以推断,当以下两个事件之一发生时,将在成功构建之后进行调试会话:
Hence we can infer that a successful build will be followed by a Debug session happens when one of these two events occurs:
- 在
Debug.Start
命令之前/之后之间触发Build.SolutionConfigurations
或Debug.StartupProjects
命令时. - 当最后一个OnAfterExecute
Debug.Start
与当前的OnBuildBegin
或OnBuildDone
之间的时间少于一秒时.
- when a
Build.SolutionConfigurations
or aDebug.StartupProjects
command is triggered in between before/afterDebug.Start
command. - when there is less than a second between the last OnAfterExecute
Debug.Start
and the currentOnBuildBegin
orOnBuildDone
.
作为一个侧面说明,当用户要求启动而不调试时,命令Debug.StartWithoutDebugging
与Debug.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知道在构建之后会进行Debug会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!