Running multiple exec tasks in parallel

2.4k Views Asked by At

I am batching multiple exec tasks in the build process. Each execution takes around one minute to complete, so I would like to run them in parallel to improve overall build performance.

The target that run multiple exec tasks:

<Target Name="CreatePackages" DependsOnTargets="Build" AfterTargets="Build">
  <Exec Command="SomeExecutable.exe %(SomeGroup.Location) </Exec>
</Target>

The ItemGroup definition:

<ItemGroup>
  <SomeGroup Include="Production">
    <Location>SomePath/Production</Location>
  </SomeGroup>
  <SomeGroup Include="Test">
    <Location>SomePath/Test</Location>
  </SomeGroup>
  <SomeGroup Include="Development">
    <Location>SomePath/Development</Location>
  </SomeGroup>
</ItemGroup>

How do I run these exec tasks in parallel?

1

There are 1 best solutions below

3
On BEST ANSWER

MSBuild doesn't parallelize on task or target level, but on project level only. You choices are to wrap each individual location in a target and call project itself in parallel (messy), or to write a custom task with TPL (System.Threading.Tasks.Parallel) and Microsoft.Build.Tasks.Exec or System.Diagnostics.Process (easy), or try YieldDuringToolExecution (technically not parallelization, but other tasks would wait less).