3

I am trying to use the MsBuild api to run a build frpm C#. The following seems to work, but only runs the build single threaded. I really need this to run multi-threaded. When I run from the command line by running msbuild I can use the /m switch to start a parallel build. How can I achieve the same programmatically?

            var logger = new BasicLogger();

            var buildParameters = new BuildParameters()
            {
                Loggers = new List<Microsoft.Build.Framework.ILogger>() { logger },
                DetailedSummary = false,
                EnableNodeReuse = false,
                OnlyLogCriticalEvents = false,
                ShutdownInProcNodeOnBuildFinish = true
            };

            var globalProperties = new Dictionary<string, string>
            {
                { "Platform", platform },
                { "Configuration", configuration },
                { "BuildInParallel", "True" } // guessed at this, doesn't seem to have an effect
            };

            var targets = new string[] { target };

            var buildRequestData = new BuildRequestData(projectFileName, globalProperties, "15.0", targets, null);
            BuildManager.DefaultBuildManager.ResetCaches();

            var buildResult = BuildManager.DefaultBuildManager.Build(buildParameters, buildRequestData);
1

1 Answer 1

1

The API mechanism to specify that builds should be attempted in parallel when possible is BuildParameters.MaxNodeCount.

To mimic the default behavior of msbuild.exe -m, you might want to specify "as many processes as the machine has logical processors:


            var buildParameters = new BuildParameters()
            {
                Loggers = new List<Microsoft.Build.Framework.ILogger>() { logger },
                DetailedSummary = false,
                EnableNodeReuse = false,
                MaxNodeCount = System.Environment.ProcessorCount,
                OnlyLogCriticalEvents = false,
                ShutdownInProcNodeOnBuildFinish = true
            };

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.