Skip to content

The shutdown of an async external application throws after the dispatcher of the Revit thread shut down #138

Closed

Description

@Nice3point

Steps to reproduce

  1. Install two add-ins whose application derives from AsyncExternalApplication. OnShutdownAsync of each awaits a task that is incomplete when the method returns, for example the stop of a host.
  2. Start Revit 2026, open a document, and exit Revit.

Seen in one of three exits of Revit 2026.5 with Nice3point.Revit.Toolkit 2026.1.4. The two other exits ended without the exception, and what shut the dispatcher down in that session is not established.

✔️ Expected Behavior

OnShutdown of an AsyncExternalApplication completes OnShutdownAsync when the dispatcher of the Revit thread has already shut down, and Revit shows no failure dialog.

❌ Actual Behavior

OnShutdown of the first add-in completes. OnShutdown of the second add-in, 13 ms later in the journal, throws from Dispatcher.PushFrame, and Revit shows External Tools - External Tool Failure for that add-in. The main window of Revit is closed at that moment.

System.InvalidOperationException: Cannot perform requested operation because the Dispatcher shut down.
   at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   at Nice3point.Revit.Toolkit.External.AsyncExternalApplication.ExecuteAsync(Func`1 asyncMethod)
   at Nice3point.Revit.Toolkit.External.AsyncExternalApplication.OnShutdown()
   at Nice3point.Revit.Toolkit.External.ExternalApplication.OnShutdown(UIControlledApplication application)
   at revitManagedAppShutdown()

ExecuteAsync calls Dispatcher.PushFrame for every task that is incomplete, and PushFrame throws once the dispatcher of the thread has finished its shutdown. The dispatcher was alive during the frame of the first add-in and had shut down before the frame of the second one.

Activity

  1. added
    bug 🐛An unexpected issue that highlights incorrect behavior
    external commands 🚪ExternalCommand, ExternalApplication, and external events
    on Nov 10, 2026
  2. self-assigned this
    on Nov 13, 2026
  3. Nice3point commented on Nov 16, 2026

    @Nice3point
    OwnerAuthor

    Correction

    The issue body states that what shut the dispatcher down is not established.
    Revit shuts it down itself:

    • UIFramework.MainWindow.MainWindow_Unloaded calls Dispatcher.BeginInvokeShutdown(DispatcherPriority.Normal).
    • In five exits of Revit 2026, the dispatcher had finished its shutdown 1.3 to 3.4 seconds before ApplicationClosing, which precedes OnShutdown.

    The body also states that the dispatcher was alive during the frame of the first add-in.
    That is not established either.
    OnShutdown pushes a frame for an incomplete task and none for a completed one, and a shutdown that returns a completed task ends without the exception in both states of the dispatcher.

    The exception then depends on whether OnShutdownAsync returns an incomplete task, not on the moment the dispatcher shuts down.
    This is an inference from the measurements: the three exits of the report were not reproduced.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug 🐛An unexpected issue that highlights incorrect behaviorexternal commands 🚪ExternalCommand, ExternalApplication, and external events

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions