我想通过代码运行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任务?
答案 0 :(得分:9)
你必须异步调用build并在循环中完成测试,你可以在其中进行进度跟踪:。
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
}
}