我希望我的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.OnBuildBegin
或BuildEvents.OnBuildDone
处理程序以某种方式查询VSIX API来了解成功构建后是否会进行调试会话?
答案 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;
}
在我的机器上,事件的顺序如下:
Debug.Start
Debug.Start
因此,如果您看到此序列,则会跟随调试会话。
完成Serge的回答。实际上我遵守了这个命令:
Debug.Start
Debug.Start
此外,如果VisualStudio估计在调试之前无需构建任何内容,则会跳过OnBuildBegin
。
OnBuildBegin
或(如果跳过)OnBuildDone
总是在OnAfterExecute Debug.Start
之后执行(在VS2010 / 2012/2013 / 2015上测试)。
Spying others命令,我可以看到两个命令Build.SolutionConfigurations
(有时还有一个或几个Debug.StartupProject
)在执行Debug.Start
之前/之后运行(我在此仅观察到此行为)在VS2013 / 2015)。
Debug.Start
Build.SolutionConfigurations
Build.SolutionConfigurations
Build.SolutionConfigurations
Build.SolutionConfigurations
Debug.StartupProjects
Debug.StartupProjects
Debug.Start
因此,我们可以推断,当发生以下两个事件之一时,成功构建后将发生调试会话:
Build.SolutionConfigurations
命令之前/之后触发Debug.StartupProjects
或Debug.Start
命令时。 Debug.Start
与当前OnBuildBegin
或OnBuildDone
之间的时间不到一秒时。作为旁注,当用户要求在没有调试的情况下启动时,命令Debug.StartWithoutDebugging
扮演与Debug.Start
相同的角色。因此,我们也可以推断成功构建后将运行(没有调试)会话