Introduction

This add-in allows users of Visual Studio 2005 to see the time taken to build a complete solution.

Background

I recently discovered a blog post that referred to an undocumented switch (/MP) in Visual Studio 2005 C++ solutions that would compile source files in multiple threads.

I wanted to determine the time saving I would get on our 30 project solution, so needed to time a solution build with and without the switch. Unfortunately, the VS2005 C++ project settings that allow you to switch on build timing will not time the entire solution, only the time to build each individual project. After suffering through two builds using a stopwatch to determine the total build times (for the record, 28m 26s without the switch, and 17m 42s with), I searched for a way to get the IDE to tell me the total build time.

Googling the problem led me to other blog and forum posts from people complaining of this missing feature (which was present back in the days of Visual Studio 6).

My solution was to use the IDE automation present in Visual Studio 2005 to add back a most welcome feature.

How the code works

The add-in was initially generated using the Visual Studio 2005 C# AddIn wizard. This created the framework on which the rest of the code could be hung.

The IDE automation model exposes two events, OnBuildBegin and OnBuildDone, which were perfect for use in the add-in as they are fired when a build is started and after it ends.

In the OnConnection event of the add-in, we get the window pane to which we intend to send output (in this case, the Build window) and add our own event handlers to the IDE:

The OnBuildBegin event first checks that we are building a solution (as opposed to a project), and further limits our timing to the Build or Rebuild All commands (we want to ignore Clean commands). If the checks are successful, we then set a flag to say we are timing the build, get the current date and time, and send that to the Build window.

The OnBuildDone event first checks that we were actually timing the build. If that is correct, we then get the current date and time, send that to the Build window, calculate the elapsed time between the start and the end of the build and, finally, send that to the Build window.

Points of interest

I found it quite difficult to determine what was available in terms of automation in the IDE. The MSDN Help on the subject appears to be sorely lacking for someone just starting out in the world of add-ins (this is only my third).

I did find a very helpful resource in the VS2005 Automation samples (downloadable from the Microsoft website) which gave me all the entry points I needed once I could browse the source code.

I restricted the build timing to the vsBuildAction events vsBuildActionBuild and vsBuildActionRebuildAll in a vsBuildScope of vsBuildScopeSolutionSolution. These, as far as I know, relate directly to the menu commands Build Solution and Rebuild Solution.

You could try taking out the restriction I imposed in the OnBuildBegin event in order to get build timing in more instances.

Alternatively, you would need to find out the scope and build action in which the build events are taking place for Start Debugging and Start Without Debugging, and alter the condition to encompass those as well.