I’m embedding MSBuild directly into a more complex build tool. The relevant code looks roughly like this:
// assume 'using Microsoft.Build.BuildEngine;' Engine e = Engine(); BuildPropertyGroup props = new BuildPropertyGroup(); props.SetProperty( 'Configuration', Config.BuildConfig ); e.BuildProjectFile( projectFile, new string[] { 'Build' }, props )
My question is how to cancel this build once it’s started, without doing something drastic like terminating the thread. Also, if the project being built is a C++ project, the build will involve at least one sub-process, so canceling the thread isn’t even going to really cancel the build.
I don’t see any cancel method on the Engine class – does someone know of a way?
It appears is that there’s no official way to do this.
For C# builds it’s not a huge deal, since they are typically really fast. The best workaround I’ve come up with for C++ builds is to locate the child processes that are created by the VC build process and terminate them, which stops the MSBuild build. This can be done using a Toolhelp32 snapshot, something like this (omitting P/Invoke garbage):
From here you can determine the parent/child relationship between processes and find the processes spawned by the app that’s invoking MSBuild.