Building Multiple Projects in Parallel

MSBuild 3.5 includes two ways to optimize build performance on multi-core or multiple processor systems, as follows:

  • By using the /maxcpucount switch on the command line.

  • By using the BuildInParallel task parameter on the MSBuild task.

/Maxcpucount Switch

The /maxcpucount switch, or /m for short, enables MSBuild 3.5 to create the specified number of MSBuild.exe processes that may be run in parallel. These processes are also known as "worker processes." Each worker process uses a separate core or processor, if any are available, to build a project at the same time as other available processors may be building other projects. For example, setting /maxcpucount to a value of "4" causes MSBuild to create four worker processes to build the project.

The following is an example of using the /maxcpucount switch on the command line.

C:\WINDOWS\Microsoft.NET\Framework\v3.5>msbuild.exe myproj.proj /maxcpucount:3

This example instructs MSBuild to build by using three worker processes. By using this configuration, three projects can built at the same time. To ensure optimal build performance, set the value of /maxcpucount to equal the number of processors or cores in the system.

BuildInParallel Task Parameter

BuildInParallel is an optional boolean parameter on a MSBuild task. When BuildInParallel is set to true (its default value), multiple worker processes are generated to build as many projects at the same time as possible. For this to work correctly, the /maxcpucount switch must be set to a value greater than 1, and the system must be at least dual-core or have two or more processors.

The following is an example, taken from microsoft.common.targets, about how to set the BuildInParallel parameter.

<PropertyGroup>
    <BuildInParallel Condition="'$(BuildInParallel)' == 
        ''">true</BuildInParallel>
</PropertyGroup>
<MSBuild
    Projects="@(_MSBuildProjectReferenceExistent)"
    Targets="GetTargetPath"
    BuildInParallel="$(BuildInParallel)"
    Properties="%(_MSBuildProjectReferenceExistent.SetConfiguration); 
        %(_MSBuildProjectReferenceExistent.SetPlatform)"
    Condition="'@(NonVCProjectReference)'!='' and 
        ('$(BuildingSolutionFile)' == 'true' or 
        '$(BuildingInsideVisualStudio)' == 'true' or 
        '$(BuildProjectReferences)' != 'true') and   
        '@(_MSBuildProjectReferenceExistent)' != ''"
    ContinueOnError="!$(BuildingProject)">
    <Output TaskParameter="TargetOutputs" 
        ItemName="_ResolvedProjectReferencePaths"/>
</MSBuild>

See Also

Concepts

Using Multiple Processors to Build Projects

Writing Multi-Processor-Aware Loggers