Sunday, December 11, 2005

UI Thread Rule in .NET Framework 2.0

Windows Forms relies on the underlying Win32® messages. Therefore, it inherits the classic Windows programming requirement that only the thread that created the window can process its messages.

The UI Thread rule states that you must never use any member of a Control on any thread other than the one that created it with exceptions of Control.Invoke, Control.BeginInvoke, Control.EndInvoke, and Control.CreateGraphics.

In his article: C# 2.0: Create Elegant Code with Anonymous Methods, Iterators, and Partial Classes, Joval Lowy stated that calls on the wrong thread will always trigger an exception under Windows Forms in the .NET Framework 2.0. I haven't yet verified this from official source, but I trust Joval. This is great!

When using multithreaded programming in Windows Forms, you should begin to use the BackgroundWork component. Check my post BackgroundWorker Component in Windows Forms 2.0.


Post a Comment

<< Home