// When updating page content, this mechanism prevents overlapped I/O; // otherwise OneNote may throw a COMException: // hrLastModifiedDateDidNotMatch (HRESULT 0x80042010) // REF: https://msdn.microsoft.com/en-us/library/office/ff966472(v=office.14).aspx async void FireAndForgetAsync(TemplateMethod template, IPageSettingsModel settings) { Action action = () => { try { SynchronizationContext.Current?.OperationStarted(); var context = PageContext.CurrentWindow(Application); XDocument page; if (context.IsMyJournalNotebook(out page)) { var sw = Stopwatch.StartNew(); template.Invoke(context, page, settings); PageTemplate.ScrollToTopOfPage(); Tracer.WriteInfoLine("{0} elapsed time: {1} ms", template.Method.Name, sw.ElapsedMilliseconds); } } catch (Exception ex) { Utils.ExceptionHandler.HandleException(ex); } finally { SynchronizationContext.Current?.OperationCompleted(); } }; await ExclusiveTaskFactory.StartNew(action, CancellationToken.None, TaskCreationOptions.DenyChildAttach, ExclusiveScheduler ).ConfigureAwait(false); }