Light the Window First: A Revit Plugin Performance Rework
The user requirement was one sentence: "when the plugin activates, the panel must light up immediately, with a loading animation inside." That one sentence led to a whole architecture rework.
The starting point: a plugin that spins
Riser Select's job is clear: pick the risers (vertical ducts, pipes, cable trays) in the current view and let the user tick riser/non-riser per category.
The experience problem was the moment of activation: you click the menu, Revit freezes and spins, and a few seconds later the window slams into existence. The bigger the model, the longer the freeze — unbearable on mechanical models with hundreds of thousands of elements.
The disease: all the work happens before the window opens
Reading the code, the cause was blunt: the window's constructor did a full set of heavy lifting —
- Collect every pipe, duct and cable tray in the current view
- Judge each one for "riser": geometry for verticality, connectors for open ends, level-span calculations…
- Only when all of it was done:
newthe window, fill the tree, ShowDialog
So after clicking the menu, the user waited for "collect + full judgment" before seeing anything. The judgment is per-element CPU work; on large models, tens of thousands of elements — all the time went there.
This is the most typical class of performance problem: heavy work sitting on the critical path of interaction. The user does not want "wait for everything, then see a window". The user wants "react immediately".
Translating a user's words into a technical plan
The user put it like this: "after activation the panel should light up at once; the tree can be empty; show 'scanning…' where the dots multiply one by one, reset, and multiply again, looping, until the result is ready."
In technical terms:
- Command activates → the window shows immediately, tree empty, a "scanning" animation on top
- Sliced scanning: collection and judgment split into small chunks; between chunks the UI refreshes animation and progress
- Scan complete → the animation disappears, results light up, buttons unlock
- Closing the window mid-scan terminates it at any time
Sounds simple — but Revit plugins have that special pit (last post's subject): random Revit API calls in WPF crash the process. How do you guarantee every slice runs inside a legal API context?
The core trick: same-thread Dispatcher slicing
Here is the most elegant step of the rework. A modal window (ShowDialog) enters a modal message pump — the window still lives on Revit's main thread; only the message loop keeps turning.
So: the command's Execute creates and ShowDialogs the window first, then queues scan tasks via Dispatcher.BeginInvoke. The modal pump picks them up one by one — the thread is still Revit's main thread, the API context intact, but each small slice returns to the pump for a breath, and the UI stays alive.
// Execute: light the window first, then dispatch the work
var window = new SelectionWindow(view, doc);
window.StartScan(); // just queues the first task
window.ShowDialog(); // modal pump spins; scanning proceeds inside it
// Inside the window: batches of 200, back to the pump between batches
private void ScanStepClassify()
{
if (Aborted()) return; // window closed → stop
var batch = TakeNext(200);
MepRiserClassifier.ClassifyBatch(batch); // the real judgment, safe here
ScanDetailText.Text = $"Judging… ({Done}/{Total})";
Dispatcher.BeginInvoke(ScanStepClassify, DispatcherPriority.Background); // queue the next batch
}
Supporting details:
- Animation: a DispatcherTimer cycles the "scanning" dots from 1 to 5 every 350 ms
- Termination: the window's Closed event sets a flag; each slice checks it — close the window and it stops
- Gray-out: during the scan, "OK" and the mode selector are disabled, preventing half-state mistakes
- Mode re-scan: switching judgment mode reruns the same animation; the user's current ticks are remembered and restored after
The result — and a by-product
After the rework: the window appears instantly, the animation spins, progress text ticks live ("Judging pipes (3400/12500)…"), a few seconds more on a huge model, Revit never frozen, cancellable at any moment.
The rework had a by-product. While chasing the performance root, we found the plugin had two layers of functionality stacked in one window — "Filter Select" and "Riser Select" — with riser classification running in full on every open, dragging down the high-frequency category filtering. So we split it into two standalone plugins: Filter Select returned to being a snappy pure category picker, and Riser Select got its own window and its own scanning animation. Chase a performance problem to its root and you often unearth an architecture problem.
Takeaways
- The essence of an experience problem is where the work sits — it is not "too slow", it is "making the user watch you work"
- Modal pump + same-thread Dispatcher slicing is the standard answer for Revit plugins that must neither crash nor block
- A user describing experience ("light it up", "the dots multiply") sounds un-technical — but translated faithfully, it is often the best plan, better than the engineer's own "add a progress bar"
