Généralement, vous devez gérer plusieurs projets en cours de construction. Cela peut être une construction de solution ou la construction d'un projet dépendant d'un autre projet.
Donc, pour savoir quand une construction réussie est terminée, vous devez utiliser une combinaison des deux événements de construction:
OnBuildProjConfigDone et OnBuildDone.
Vous aurez également besoin d'une variable membre pour suivre l'état de construction global.
Votre gestionnaire OnBuildProjConfigDone sera appelé pour chaque projet qui sera construit, et il recevra un booléen pour vous dire si la construction du projet a réussi. Affectez ce résultat à votre variable membre pour garder une trace du statut global.
Enfin, votre gestionnaire OnBuildDone sera appelé. Ici, vous pouvez regarder votre variable membre pour voir si la construction du projet a échoué.
Voici un exemple de code d'une extension que j'ai écrite pour VS2012. L'extension fournit une commande "custom build" qui génère le projet actif et lance le débogueur si la génération a réussi.
private bool _overallBuildSuccess;
private bool _customBuildInProgress;
private void CustomBuild_MenuItemCallback(object sender, EventArgs e)
{
// Listen to the necessary build events.
DTE2 dte = (DTE2)GetGlobalService(typeof(SDTE));
dte.Events.BuildEvents.OnBuildDone += BuildEvents_OnBuildDone;
dte.Events.BuildEvents.OnBuildProjConfigDone += BuildEvents_OnBuildProjConfigDone;
try
{
// Build the active project.
_customBuildInProgress = true;
dte.ExecuteCommand("Build.BuildSelection");
}
catch (COMException)
{
_customBuildInProgress = false;
WriteToOutputWindow("Build", "Could not determine project to build from selection");
}
}
private void BuildEvents_OnBuildProjConfigDone(string project, string projectConfig, string platform, string solutionConfig, bool success)
{
// Ignore this build event if we didn't start it.
if (!_customBuildInProgress)
{
return;
}
// Keep track of the overall build success.
_overallBuildSuccess = success;
}
private void BuildEvents_OnBuildDone(EnvDTE.vsBuildScope scope, EnvDTE.vsBuildAction action)
{
// Ignore this build event if we didn't start it.
if (!_customBuildInProgress)
{
return;
}
_customBuildInProgress = false;
if (_overallBuildSuccess)
{
// Launch the debugger.
DTE2 dte = (DTE2)GetGlobalService(typeof(SDTE));
dte.ExecuteCommand("Debug.Start");
}
else
{
WriteToOutputWindow("Build", "Custom build failed.");
}
}
private void WriteToOutputWindow(string paneName, string message)
{
DTE2 dte = (DTE2)GetGlobalService(typeof(SDTE));
Window window = dte.Windows.Item(EnvDTE.Constants.vsWindowKindOutput);
OutputWindow outputWindow = (OutputWindow)window.Object;
OutputWindowPane targetPane = outputWindow.OutputWindowPanes.Cast<OutputWindowPane>()
.FirstOrDefault(x => x.Name.ToLower() == paneName.ToLower());
if (targetPane == null)
{
targetPane = outputWindow.OutputWindowPanes.Add(paneName);
}
targetPane.Activate();
outputWindow.ActivePane.OutputString(message);
outputWindow.ActivePane.OutputString(Environment.NewLine);
}
Dans mon cas, OnBuildDone n'a jamais été déclenché, mais le OnBuildProjConfigDone a fonctionné correctement –
@DinisCruz Parfois, OnBuildDone ne se déclenche pas si vous ne possédez pas de référence à dte.Events.BuildEvents – Artiom
Voici un exemple complet de code pour ce faire: https://github.com/edsykes/VisualStudioBuildEvents –