Keep your WPF UI responsive

Hopefully, everyone writing Windows applications understands the value of keeping the UI responsive. That is, we all know that it is important to avoid long-running or long-waiting work in the UI thread, because it prevents the window from doing anything - it can’t respond to keystrokes or clicks; it can’t even update its display. So, we make sure that any work we do is so fast that the user won’t notice, or we move that work into background threads.

Still, it is easy to miss something, especially when an operation usually completes quickly, but can be much slower under certain circumstances. Until recently, we could get away with it from time to time - in fact, we might display a “Working” message right before we started the work, or we might set the mouse cursor to the “hourglass” on the hopes that the user won’t get too impatient and terminate the application.

These days, however, Windows is much less forgiving. Under Windows Vista, at least, if a window is not responsive for five seconds or so, the window flickers a bit and Windows adds the humiliating “(Not Responding)” message to the window caption.

WinFormsAppNotResponding

Even so, surely the user could deal with that from time to time. A temporarily frozen user interface isn’t the end of the world; after all, that program is working hard! Just let it finish and everything will be fine again.

Unfortunately, things get much worse when running a WPF application under Windows Vista with Aero enabled (the glassy window captions). If that application becomes unresponsive, you get the “(Not Responding)” message, as well as something we like to call the “black screen of death” - the entire content of the window goes pitch black.

WpfAppNotResponding

This is surely unacceptable - nothing says “terminate me” like a black window. In the end, I suppose this will be best for the user, because we’re trying extra hard to make sure that nothing we do on the UI could ever take five seconds. But it’s certainly a lot harder than displaying the trusty old hourglass.

Posted by Ed Ball on October 15, 2008