2

我想通过代码运行 MSBUILD,并在构建过程中获取构建的异步状态(类似于 TeamCity 或任何其他构建运行程序)。

我正在使用以下代码:

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

有没有办法通过代码获取当前正在执行的 MSBUILD 任务?

4

1 回答 1

9

您必须异步调用 build 并在循环中测试 iscomplete ,您可以在其中进行进度跟踪:。

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           
        } 
    }
于 2012-08-12T10:59:14.240 回答