Let Word document registration complete before selection retry
This commit is contained in:
parent
2f0f5a4b74
commit
f0d1f6be2a
@ -20,6 +20,7 @@ namespace PlotDirector.WordAddIn
|
||||
private readonly Word.Application application;
|
||||
private readonly Word.ApplicationEvents4_Event events;
|
||||
private readonly Dictionary<Word.Document, LocalDocument> documents = new Dictionary<Word.Document,LocalDocument>();
|
||||
private readonly HashSet<Word.Document> pendingOpen = new HashSet<Word.Document>();
|
||||
private readonly HashSet<Word.Document> closing = new HashSet<Word.Document>();
|
||||
private readonly WordAccessScheduler scheduler = new WordAccessScheduler();
|
||||
private readonly Control dispatch = new Control();
|
||||
@ -44,6 +45,7 @@ namespace PlotDirector.WordAddIn
|
||||
private LocalDocument active;
|
||||
private bool disposed;
|
||||
private long busyFailures;
|
||||
private string lastWordError;
|
||||
private int documentCollectionGeneration;
|
||||
private double lastBatchMs;
|
||||
private string lastResult = "Waiting for local structural snapshot";
|
||||
@ -53,7 +55,7 @@ namespace PlotDirector.WordAddIn
|
||||
+ "\nServer authentication / prose sync: unavailable\nOpen sessions: " + documents.Count
|
||||
+ "\nIPC queued: " + link.QueueDepth + "\nWord work queued: " + scheduler.Depth
|
||||
+ "\nLast UI batch ms: " + lastBatchMs.ToString("F2") + "\nMax recorded COM call ms: " + (operationTimes.Count == 0 ? 0 : operationTimes.Max()).ToString("F2")
|
||||
+ "\nCOM busy/errors: " + busyFailures + "\n" + lastResult;
|
||||
+ "\nCOM busy/errors: " + busyFailures + (lastWordError == null ? "" : " (last: " + lastWordError + ")") + "\n" + lastResult;
|
||||
|
||||
public WordAdapter(Word.Application application, Action invalidateRibbon)
|
||||
{
|
||||
@ -84,7 +86,11 @@ namespace PlotDirector.WordAddIn
|
||||
});
|
||||
};
|
||||
scheduler.Measured += (_, elapsed) => lastBatchMs = elapsed;
|
||||
scheduler.Failed += _ => { busyFailures++; scheduler.PrioritizeAuthor(TimeSpan.FromSeconds(1)); };
|
||||
scheduler.Failed += error => {
|
||||
busyFailures++;
|
||||
lastWordError = error.GetType().Name + " 0x" + error.HResult.ToString("X8");
|
||||
scheduler.PrioritizeAuthor(TimeSpan.FromSeconds(1));
|
||||
};
|
||||
events = (Word.ApplicationEvents4_Event)application;
|
||||
events.DocumentOpen += Open;
|
||||
events.NewDocument += Open;
|
||||
@ -130,9 +136,10 @@ namespace PlotDirector.WordAddIn
|
||||
}
|
||||
private void Open(Word.Document document)
|
||||
{
|
||||
if (documents.ContainsKey(document) || documents.Count >= ProtocolVersion.MaxDocuments) return;
|
||||
if (documents.ContainsKey(document) || documents.Count >= ProtocolVersion.MaxDocuments || !pendingOpen.Add(document)) return;
|
||||
documentCollectionGeneration++;
|
||||
scheduler.Post("open:" + Guid.NewGuid().ToString("N"), () => {
|
||||
if (!scheduler.Post("open:" + Guid.NewGuid().ToString("N"), () => {
|
||||
pendingOpen.Remove(document);
|
||||
if (documents.ContainsKey(document) || documents.Count >= ProtocolVersion.MaxDocuments) return;
|
||||
var state = new LocalDocument(document);
|
||||
state.Identity = ReadGuidProperty(document,"PlotDirector.Native.DocumentGuid") ?? Guid.NewGuid();
|
||||
@ -141,8 +148,8 @@ namespace PlotDirector.WordAddIn
|
||||
state.BookId = ReadPositiveProperty(document,"PlotDirector.Native.BookID");
|
||||
state.End = DocumentEnd(document); state.WasSaved = Com(() => document.Saved);
|
||||
documents.Add(document,state); link.Register(Descriptor(state));
|
||||
invalidateRibbon();
|
||||
});
|
||||
ScheduleSelection();
|
||||
})) pendingOpen.Remove(document);
|
||||
}
|
||||
private void Activate(Word.Document document,Word.Window window) { Open(document); ScheduleSelection(); }
|
||||
private void SelectionChanged(Word.Selection selection) { ScheduleSelection(); }
|
||||
@ -161,7 +168,7 @@ namespace PlotDirector.WordAddIn
|
||||
{
|
||||
selection = Com(() => application.Selection);
|
||||
var document = Com(() => selection.Document);
|
||||
if (!documents.TryGetValue(document,out var state)) { Open(document); ScheduleSelection(); return; }
|
||||
if (!documents.TryGetValue(document,out var state)) { Open(document); return; }
|
||||
range = Com(() => selection.Range);
|
||||
var start = Com(() => range.Start); var end = Com(() => range.End);
|
||||
var documentEnd = DocumentEnd(document); var saved = Com(() => document.Saved);
|
||||
@ -430,7 +437,7 @@ namespace PlotDirector.WordAddIn
|
||||
if(disposed)return;disposed=true;timer.Stop();timer.Dispose();scheduler.Clear();
|
||||
events.DocumentOpen-=Open;events.NewDocument-=Open;events.DocumentChange-=ScheduleSelection;events.WindowActivate-=Activate;
|
||||
events.WindowSelectionChange-=SelectionChanged;events.DocumentBeforeClose-=BeforeClose;events.DocumentBeforeSave-=BeforeSave;
|
||||
link.Dispose();documents.Clear();closing.Clear();dispatch.Dispose();
|
||||
link.Dispose();documents.Clear();pendingOpen.Clear();closing.Clear();dispatch.Dispose();
|
||||
}
|
||||
private sealed class LocalDocument
|
||||
{
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user