CMSC 498B: Developing User Interfaces - Spring 2005Threaded GUI Apps | |||||||||
Threaded GUI AppsWhat are threads? Conceptual simultaneous execution paths through your code. Might be run through alternation or actually simultaneously (if on a multi-processor machine)./* * A C# program that demonstrates simple use of threads * * Ben Bederson, March 9, 2005 */ using System; using System.Threading; namespace ThreadSample { public class ThreadSample { int value; public ThreadSample(int value) { this.value = value; } public void Run() { do { Console.WriteLine("Thread " + value); Thread.Sleep(300); } while (true); } static public void Main() { ThreadSample sample1 = new ThreadSample(1); ThreadSample sample2 = new ThreadSample(2); Thread thread1 = new Thread(new ThreadStart(sample1.Run)); Thread thread2 = new Thread(new ThreadStart(sample2.Run)); thread1.Start(); thread2.Start(); } } } What does this have to do with user interfaces?
=> You are not allowed to manipulate Forms objects outside the Event thread => Your own code needs to be thread safe if you want to modify your model outside of the Event thread, but access it within the Event thread. => From another thread, you can access the Event dispatch thread with Control.Invoke() and Control.BeginInvoke(). How to have slow event-driven operations? What if they need to update the display? ... counterThread = new Thread(new ThreadStart(IncrementCounter)); counterThread.Start(); ... // This gets called in a special thread protected void IncrementCounter() { do { // Force update to happen in event thread BeginInvoke(new MethodInvoker(UpdateCounterDisplay)); counterValue++; Thread.Sleep(100); } while (true); } protected void UpdateCounterDisplay() { counter.Text = counterValue.ToString(); } Source: ThreadedGUI.zip Or, what if the GUI needs to be updated as a result of a non-GUI generated event (like network traffic)? How is BeginInvoke() implemented? C# Toolkits and Threads (from the QuickStart guide)Windows Forms controls can only execute on the thread on which they were created, that is, they are not thread-safe. If you want to get or set properties, or call methods, on a control from a background thread, the call must be marshaled to the thread that created the control. There are five functions on a control that are safe to call from any thread: InvokeRequired, Invoke, BeginInvoke, EndInvoke and CreateGraphics. For all other method calls, you should use one of the invoke methods. By default, Windows marshals the calls for you. However, if you are making multiple calls to a control, it is much more efficient to create a method that executes those calls and make the one cross-thread call yourself. You make the cross-thread call by calling one of the Control.Invoke methods. The Invoke methods take a reference to a delegate. Typically, this delegate is an instance of the MethodInvoker delegate.
Java SwingIdentical threading model for the UI Invoke => SwingUtilities.invokeAndWait() Java's GUI toolkit. Learn about it from the Java Tutorial: The Swing Components from the Java Tutorial: Learn more about Java and Threads from the Java Tutorial:
|