2012-08-12 47 views
2

我想通過代碼運行的MSBuild,並獲得建造的異步狀態,因爲它的進展(類似於TeamCity的或任何其他構建亞軍一樣)。通過代碼運行的MSBuild並獲得異步進度

我使用下面的代碼:

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任務?

回答

9

你必須調用建立asyncronously和在一個循環中,你可以讓你的進度跟蹤測試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   
     } 
    } 
+0

我實際上是在考慮設置一個MSBUILD日誌提供程序,並觀察它的構建完成消息嗎? – Doug 2012-10-23 10:16:09

+0

我會說這絕對是正確的答案 – OrPaz 2014-02-24 15:00:33

相關問題