從 IDE 中啟動組建
自訂專案系統必須使用 IVsBuildManagerAccessor 來啟動組建。 本主題說明這麼做的原因並概述相關程序。
平行組建和執行緒
Visual Studio 允許需要居中協調通用資源存取權的平行組建。 專案系統可以非同步方式執行組建,但是這樣的系統絕對不能從提供給組建管理員的回呼中呼叫組建函式。
如果專案系統修改環境變數,就必須將組建的 NodeAffinity 設定為 OutOfProc。 這表示您將無法使用主物件,因為這些物件必須有同處理序節點才能使用。
使用 IVSBuildManagerAccessor
下列程式碼概略說明專案系統可以用來啟動組建的方法:
public bool Build(Project project, bool isDesignTimeBuild)
{
// Get the accessor from the IServiceProvider interface for the
// project system
IVsBuildManagerAccessor accessor =
serviceProvider.GetService(typeof(SVsBuildManagerAccessor)) as
IVsBuildManagerAccessor;
bool releaseUIThread = false;
try
{
if(accessor != null)
{
// Claim the UI thread under the following conditions:
// 1. The build must use a resource that uses the UI thread
// or,
// 2. The build requires the in-proc node AND waits on the
// UI thread for the build to complete
if(NeedsUIThread)
{
int result = accessor.ClaimUIThreadForBuild();
if(result != S_OK)
{
// Not allowed to claim the UI thread right now
return false;
}
releaseUIThread = true;
}
if(isDesignTimeBuild)
{
// Start the design time build
int result = accessor.BeginDesignTimeBuild();
if(result != S_OK)
{
// Not allowed to begin a design-time build at
// this time. Try again later.
return false;
}
}
}
bool buildSucceeded = false;
// perform project-system specific build set up tasks
// Create your BuildRequestData
// This assumes a IHostServices variable (hostServices) set
// to your host services. If you don't use a project instance
// (you build from a file for example) then use another
// constructor.
BuildRequestData requestData = new
BuildRequestData(project.CreateProjectInstance(),
"myTarget", hostServices,
BuildRequestData.BuildRequestDataFlags.None);
// Mark your your submission as Pending
BuildSubmission submission =
BuildManager.DefaultBuildManager.
PendBuildRequest(requestData);
// Register the loggers in BuildLoggers
if (accessor != null)
{
foreach (ILogger logger in BuildLoggers)
{
accessor.RegisterLogger(submission.SubmissionId,
logger);
}
}
BuildResult buildResult = submission.Execute();
return buildResult;
}
// Clean up resources
finally
{
if(accessor != null)
{
// Unregister the loggers, if necessary.
accessor.UnregisterLoggers(submission.SubmissionId);
// Release the UI thread, if used
if(releaseUIThread)
{
accessor.ReleaseUIThreadForBuild();
}
// End the design time build, if used
if(isDesignTimeBuild)
{
accessor.EndDesignTimeBuild();
}
}
}
}