The controls provided by Windows are not designed to be thread-safe; they are intended to be accessed only the by thread which created them, namely the UI thread. But that UI thread is engaged in an endless loop, waiting for and dispatching the messages sent to its windows, and will therefore never be available to do anything else. How then can a worker thread interact with the user interface if it cannot safely access the controls by itself, and if the only thread allowed to do so will never get available ?
In Windows Forms, this problem is addressed by WindowsFormsSynchronizationContext. Any method call sent to this context is automatically translated into a Windows message, which is then posted to the UI thread's message pump. Eventually, the UI thread processes this message, and executes the requested method. That is how a worker thread can get the attention of the UI thread, and have it safely access the user interface controls on it's behalf.
Saturday, August 16, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment