共用方式為


同時建置多個專案

MSBuild 3.5 提供下列兩個方法來最佳化多核心或多處理器系統上的建置效能:

  • 藉由在命令列上使用 /maxcpucount 參數。

  • 藉由在 MSBuild 工作上使用 BuildInParallel 工作參數。

/Maxcpucount 參數

/maxcpucount 參數 (簡寫為 /m) 可讓 MSBuild 3.5 建立指定數目的同時執行 MSBuild.exe 處理序。這些處理序也稱為「背景工作處理序」。每個背景工作處理序都會使用不同的核心或處理器 (如可用) 來建置專案,而同時可能也有其他可用的處理器正在建置其他專案。 例如,將 /maxcpucount 設為值 "4" 時,MSBuild 會建立四個背景工作處理序來建置專案。

以下是在命令列上使用 /maxcpucount 參數的範例。

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

這個範例指示 MSBuild 使用三個背景工作處理序進行建置。 藉由使用此組態,可同時建置三個專案。 為確保最佳建置效能,請將 /maxcpucount 的值設成等於系統處理器或核心的數目。

BuildInParallel 工作參數

BuildInParallel 是 MSBuild 工作上的選擇性布林值參數。 BuildInParallel 設為 true (預設值) 時,就會產生多個背景工作處理序,以同時建置任何數目的專案。 為使 /maxcpucount 參數正確運作,其值必須設為大於 1,且系統必須至少是雙核心或有兩個以上的處理器。

以下是取自於 microsoft.common.targets 的範例,示範如何設定 BuildInParallel 參數。

<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>

請參閱

概念

使用多個處理器來建置專案

撰寫能夠辨識多處理器的記錄器