Pete Montgomery
Pete Montgomery

Reputation: 4100

How to run tasks in parallel in MSBuild

Unless I've grossly misunderstood MSBuild, tasks are executed in the document order in which they appear within a 'Target' node.

I'd like to be able to specify that two tasks (such as xcopy tasks) could run in parallel. I was expecting there to be a 'Parallel' task or something...?

Upvotes: 36

Views: 34807

Answers (6)

Jonathan
Jonathan

Reputation: 7128

For C++, you can do it with

  •  <MultiProcessorCompilation>true</MultiProcessorCompilation>
    

Which can be found if you search 'multi' under C++ > All Options of the Property pages of the project

More info here: https://devblogs.microsoft.com/cppblog/recommendations-to-speed-c-builds-in-visual-studio/

Upvotes: 0

Sayed Ibrahim Hashimi
Sayed Ibrahim Hashimi

Reputation: 44342

As was stated, you cannot parallelise at the task level or even at the target level. MSBuild only will build projects (i.e. MSBuild project files) in parallel. So you have to use the MSBuild task with multiple projects specified and the BuildInParallel attribute should be set to true. Also make sure that when the build is invoked on the command line that the /m switch is sent it.

My Book: Inside the Microsoft Build Engine : Using MSBuild and Team Foundation Build

Upvotes: 22

Parag Doke
Parag Doke

Reputation: 863

Here is an example of a way to run msbuild targets in parallel. The idea is same ... presenting this msbuild file itself as a project to build. I copied it from my own question: Evaluate item defined in msbuild task via C#

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0" DefaultTargets="Build">
<Target Name="Build" DependsOnTargets="PrepareEnvironmentForBuild;MapDrives">
    <Exec Command="$(MSBuildBinPath)\msbuild /nologo /clp:Verbosity=quiet $(MSBuildThisFileFullPath) /t:TargetWithConfidentialSteps"/>
    <ItemGroup>
        <StepsToRunInParallel Include="$(MSBuildThisFileFullPath)">
            <Properties>TargetToInvoke=ParallelTarget1</Properties>
        </StepsToRunInParallel>
        <StepsToRunInParallel Include="$(MSBuildThisFileFullPath)">
            <Properties>TargetToInvoke=ParallelTarget2</Properties>
        </StepsToRunInParallel>
    </ItemGroup>
    <MSBuild Projects="@(StepsToRunInParallel)" BuildInParallel="true" StopOnFirstFailure="true" Targets="InvokeInParallelWithinThisProject"/>

</Target>
<Target Name="InvokeInParallelWithinThisProject">
    <MSBuild Projects="$(MSBuildThisFileFullPath)" Targets="$(TargetToInvoke)" StopOnFirstFailure="true"/>
</Target>
<Target Name="ParallelTarget1">
    <Message Text="Hello from ParallelTarget1"/>
</Target>
<Target Name="ParallelTarget2">
    <Message Text="Hello from ParallelTarget2"/>
</Target>
<Target Name="PrepareEnvironmentForBuild">
    <Message Text="Hello from PrepareEnvironmentForBuild"/>
</Target>
<Target Name="MapDrives">
    <Message Text="Hello from MapDrives"/>
</Target>
<Target Name="TargetWithConfidentialSteps">
    <Message Text="Hush! Verbosity on the wrapper does not affect the Exec call." Importance="High"/>
</Target>

Upvotes: 6

Mike Fourie
Mike Fourie

Reputation: 491

Try the new parallel task in the MSBuild Extension Pack - http://mikefourie.wordpress.com/2012/02/29/executing-msbuild-targets-in-parallel-part-1

Upvotes: 39

adrianbanks
adrianbanks

Reputation: 83004

MSBuild has a /m command line switch to tell it the maximum number of concurrent processes to build with. The default value is 1. /m:x will use x processes. /m will use the number of processors on computer.

I've used this as part of a shortcut in Visual Studio to run builds quicker by compiling projects in parallel. Scott Hanselman has a few posts about it here and here.

Upvotes: 13

Paul Sonier
Paul Sonier

Reputation: 39510

I don't think there is a way to do this other than using some external tool. I suspect this is because this introduces potential dependency problems that MS chooses to ignore for the time being, allowing external tool vendors to tackle the problem (and charge quite a bit of money for it).

Upvotes: 3

Related Questions