2

I am wanting to run MSBUILD through code, and get asynchronous status of the build as it progresses (similar to TeamCity or any other build runner does).

I am using the following code:

var projectFileName = @"...\MyApplication\MyApplication.sln";
ProjectCollection pc = new ProjectCollection();
var GlobalProperty = new Dictionary<string, string>();
GlobalProperty.Add("Configuration", "Debug");
GlobalProperty.Add("Platform", "x86");

var buildRequest = new BuildRequestData(projectFileName, GlobalProperty, null, new string[] { "Build" }, null);

var buildResult = BuildManager.DefaultBuildManager.Build(new BuildParameters(pc), buildRequest);

Is there a way to get the current executing MSBUILD task through code?

Doug
  • 6,460
  • 5
  • 59
  • 83

1 Answers1

9

you have to call build asyncronously and to test iscomplete in a loop where you can make your progress tracking:.

private void button1_Click(object sender, EventArgs e)
    {
        var projectFileName = @"...\MyApplication\MyApplication.sln";   
       ProjectCollection pc = new ProjectCollection(); 
       var GlobalProperty = new Dictionary<string, string>(); 
       GlobalProperty.Add("Configuration", "Debug"); 
       GlobalProperty.Add("Platform", "x86");
       BuildManager.DefaultBuildManager.BeginBuild(new BuildParameters(pc));
       BuildRequestData request = new BuildRequestData(projectFileName, GlobalProperty, null, new string[] { "Build" }, null); 
       BuildSubmission submission = BuildManager.DefaultBuildManager.PendBuildRequest(request); 
        submission.ExecuteAsync(null, null);
        int cpt = 0;
        while (!submission.IsCompleted)
        {
            cpt++;
            textBox1.Text = cpt.ToString();
        }


        BuildManager.DefaultBuildManager.EndBuild();   
        // If the build failed, return an error string.    
        if (submission.BuildResult.OverallResult == BuildResultCode.Failure)      
        {                 //do some error task           
        } 
    }
Hassan Boutougha
  • 3,871
  • 1
  • 17
  • 17
  • I was actually considering setting up a MSBUILD log provider and watching it for build complete messages? – Doug Oct 23 '12 at 10:16