Running MSBUILD through code and getting asynchronous progress

蓝咒 提交于 2019-12-22 18:01:11

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/11921184/running-msbuild-through-code-and-getting-asynchronous-progress

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!