fix: close all automation-opened documents

This commit is contained in:
codex
2026-08-03 17:10:02 +03:00
parent ef615a1f8e
commit 33f91c26aa
2 changed files with 15 additions and 9 deletions
@@ -129,6 +129,7 @@ internal sealed class FirstLimitedApplyAutomationCoordinator
}
private FirstApplyAutomationConfiguration? config;
private Document? document;
private readonly List<Document> automationOpenedDocuments = [];
private readonly List<string> completed = [];
private int step;
private int? before, after1, after2, afterUndo;
@@ -159,9 +160,10 @@ internal sealed class FirstLimitedApplyAutomationCoordinator
var project = PlantProject.LoadProject(projectXml, false, string.Empty, string.Empty);
PlantApplication.SetCurrentProject(project, true);
transitions.Add("ProjectActivated");
if (!string.IsNullOrWhiteSpace(config.ModelDrawingPath)) AcadApplication.DocumentManager.Open(config.ModelDrawingPath, false);
if (!string.IsNullOrWhiteSpace(config.ModelDrawingPath)) automationOpenedDocuments.Add(AcadApplication.DocumentManager.Open(config.ModelDrawingPath, false));
transitions.Add("DocumentOpenStarted");
document = AcadApplication.DocumentManager.Open(config.OrthoDrawingPath, false);
automationOpenedDocuments.Add(document);
AcadApplication.DocumentManager.MdiActiveDocument = document;
transitions.Add("DocumentOpened");
document.CommandEnded += OnCommandEnded;
@@ -315,12 +317,15 @@ internal sealed class FirstLimitedApplyAutomationCoordinator
exitHandlerSubscribed = false;
// Do not close a cached automation document and then reuse it. Resolve
// the currently-live document only at the final quiescent callback.
var active = AcadApplication.DocumentManager.MdiActiveDocument;
if (active is not null)
// Only documents opened by this coordinator may be closed. Keep no
// document reference after closing it; DocumentDestroyed can run
// immediately as part of CloseAndDiscard.
foreach (var opened in automationOpenedDocuments.ToArray().Reverse())
{
transitions.Add("ActiveDocumentCloseRequested");
active.CloseAndDiscard();
transitions.Add("AutomationDocumentCloseRequested");
opened.CloseAndDiscard();
}
automationOpenedDocuments.Clear();
// Application.Quit alone leaves the script host alive. Close the
// live document first, then request normal application shutdown.
transitions.Add("ApplicationQuitRequested");