private static void ExecuteWithAppsShutDown(string abbrevOfDefaultAppToStart, Func<ProjectId> action)
{
bool allowFinalShutdownOrigValue = s_allowFinalShutdown;
s_allowFinalShutdown = false; // don't shutdown when we close all windows!
// Remember which apps were running (and the order in which to restore them)
List<string> appsToRestore = new List<string>();
// If the requested default application is running, then add it as the first app to restore
if (GetAppFromAppNameOrAbbrev(abbrevOfDefaultAppToStart) != null)
appsToRestore.Add(abbrevOfDefaultAppToStart);
if (s_flexApp != null && !appsToRestore.Contains(FwUtils.ksFlexAbbrev))
appsToRestore.Add(FwUtils.ksFlexAbbrev);
if (s_teApp != null && !appsToRestore.Contains(FwUtils.ksTeAbbrev))
appsToRestore.Add(FwUtils.ksTeAbbrev);
if (appsToRestore.Count == 0)
{
if (abbrevOfDefaultAppToStart.Equals(FwUtils.ksTeAbbrev, StringComparison.InvariantCultureIgnoreCase) &&
FwUtils.IsTEInstalled)
{
appsToRestore.Add(FwUtils.ksTeAbbrev);
}
else
appsToRestore.Add(FwUtils.ksFlexAbbrev);
}
// Now shut down everything (windows, apps, cache, etc.)
GracefullyShutDown();
if (s_applicationExiting)
{
// Something bad must have happened because we are shutting down. There is no point in
// executing the action or in restarting the applications since we have no idea what
// state the applications/data are in. (FWR-3179)
Debug.Assert(s_allowFinalShutdown, "If something bad happened, we should be allowing application shutdown");
return;
}
try
{
// Run the action
ProjectId projId = action();
if (projId == null)
return;
s_projectId = null; // Needs to be null in InitializeFirstApp
// Restart the default app from which the action was kicked off
FwApp app = GetOrCreateApplication(new FwAppArgs(appsToRestore[0],
projId.Handle, projId.ServerName, string.Empty, Guid.Empty));
if (!InitializeFirstApp(app, projId))
return;
s_projectId = projId; // Process needs to know its project
// Reopen other apps if necessary (shouldn't ever be more then one) :P
for (int i = 1; i < appsToRestore.Count; i++)
{
app = GetOrCreateApplication(new FwAppArgs(appsToRestore[i], projId.Handle,
projId.ServerName, string.Empty, Guid.Empty));
InitializeApp(app, null);
}
}
finally
{
s_allowFinalShutdown = allowFinalShutdownOrigValue; // mustn't suppress any longer (unless we already were).
ExitIfNoAppsRunning();
}
}