Skip to main content
FridayPlus 图标FridayPlus
← Back to updates
2026-11-02Blog

The Case of the Instant Revit Crash: You Click a WPF Button and the Process Is Gone

A real crash from the Duct Converter plugin — symptom, root cause, fix. The subtlety of this pit deserves every Revit plugin developer's attention.

The symptom: no error, the whole of Revit evaporates

The user report was short: clicking the "clear" button in the plugin panel made Revit vanish instantly. No error dialog, no stack trace, nothing. Mid-project, hours of work hanging between autosaves.

A "silent crash" like this is the nastiest kind. The plugin code has try-catch everywhere; any exception should be caught and shown as a TaskDialog. Nothing was shown — meaning the exception never reached a catch.

The forensics: one line in the dump

Windows Event Viewer had Revit's crash record, and the program log had the key line:

Invalid call to Revit API! Revit is currently not within an API context

A Revit API call was made outside an API context. That line is the key to the whole case. But it raises an even stranger question: the code had try-catch — why wasn't the exception caught, and why did it kill the process instead?

The root cause: the exception escaped through a channel your catch cannot cover

Revit API calls have "legal hours" — you are inside an API context only during an external command (IExternalCommand.Execute) or an event callback.

Event handlers of a WPF window (a button's Click, the window's Closing, a textbox's TextChanged) run in WPF's own message pump. There:

  1. The external command returned long ago; the API context is gone
  2. The user clicks the button → the WPF handler runs → it calls the Revit API → Revit throws
  3. This exception is not thrown from the Execute call stack — it comes out of WPF's Dispatcher message loop. Your try-catch wraps Execute; it cannot cover this
  4. The exception escapes the Dispatcher → CLR default policy → process termination

In one sentence: what crashed was not your feature — it was "the right API at the wrong moment".

The scene of the crime: the "clear" button's handler did two things — unsubscribed from the Idling event, and opened a Transaction to modify the model. Both are API operations, both outside an API context.

The fix: separate "what we want" from "what we may do now"

The solution accepts reality: inside a WPF event handler, only leave notes — never act.

Model changes → ExternalEvent:

// In Execute (API context alive): create the event handler
_handler = new ApplySizeHandler();
_event = ExternalEvent.Create(_handler);   // must be created here!

// In the WPF button (no API context): just ring the bell
private void ApplyButton_Click(object s, RoutedEventArgs e)
{
    _handler.Size = ReadInput();   // prepare data only
    _event.Raise();                // book one API-context slot
}

ExternalEvent.Raise() means "next time Revit idles, run me once inside an API context". The real Transaction runs in the handler's Execute(UIApplication) — that is the safe place.

Unsubscribing from Idling → a flag, deferred:

// Closing only raises the flag
private void Window_Closing(object s, CancelEventArgs e)
{
    _pendingUnsubscribe = true;   // do not unsubscribe; note that we should
}

// The next Idling callback (inside an API context) unsubscribes safely
private void OnIdling(object sender, IdlingEventArgs e)
{
    if (_pendingUnsubscribe) { Unsubscribe(); return; }
    // ... normal work ...
}

Post-mortem: why AI stepped in this pit too

One detail worth telling: this bug was written by AI.

AI knows the Revit API and knows WPF; every API call, taken alone, was correct. What was wrong was the timing. This kind of cross-domain constraint (Revit's context rules × WPF's message pump) does not occur to AI when generating code — unless you have told it.

After fixing, we did two things:

  1. Wrote the full mechanism and the correct pattern into the library's pit notes
  2. Added a top-priority rule to AI's development skill: no Revit API calls inside WPF event handlers, with a self-check (search every WPF event-handler method body; Document, Transaction or UIApplication appearing there is a violation)

The checklist has intercepted this pattern several times since. The same pit, never stepped in again.

That is the core lesson of working with AI: AI does not learn from mistakes unless you turn the mistake into a rule.

The next post is about performance: a window that had to scan hundreds of thousands of elements on open — how we made it "window first, scanning never blocks".