Gui Thread

A DesignPattern

Problem

In what thread should the GUI events be processed?

Context

A MultiThreaded? event-based GUI, typically a Swing application or any GUI application needing to perform some background processing using several threads and reporting the progress to the user.

Solution

All events should be dispatched and processed through a clearly-documented GuiThread. In a Windows application, this is typically the MainThread?. Ideally, it is also the thread that created all the GUI widgets. If it is not the case, you might have a problem if the GUI library is not thread-safe.

Any background thread should report its progress to the GUI by posting messages through the GuiThread event queue. A background thread should never access the GUI immediately, as most GUI libraries are not ThreadSafe.

Most GUI frameworks allow you to dispatch events through its EventQueue. In Java AWT, you can dispatch a Runnable in the AWT EventThread?. On Windows, you can post the WM_USER message.

Ideally, TheGuiThreadIsTheMainThread

-- PhilippeDetournay


See also: EventQueue


CategoryPattern CategoryEvents


EditText of this page (last edited February 26, 2008) or FindPage with title or text search