javac#multithreadinguser-interfacegui-designer

Are the GUI components in C# and Java run by a separate thread each?


I am wondering how the GUI components in C# and Java are working behind the scenes. To me it seems like each component needs to have it's own thread that is listening on it. If you have the Frame, ButtonA and ButtonB for example, are there three separate threads then listening on each of them? Like there is a Frame thread listening on the Frame, a ButtonA thread listening on ButtonA and a third thread listening on ButtonB? How can they be listened to at the same time otherwise? I also wonder if it is bad practice to use the GUI designer in Visual Studio. Is it unprofessional to use the GUI designer tool and should I learn to code the GUI from scratch? Should I know how to code the GUI in C#? I already know Swing quite well in Java.

Thanks!


Solution

  • I don't know about C# but AWT, Swing and JavaFX are single-threaded as most GUI solutions today are. The main thread in Swing e.g. is called the event dispatch thread and its name pretty much tells you what it does. It basically pulls from a queue of events and dispatches them to corresponding listener components. E.g. if you click on a button the operating system will generate an event that will be enqeued in the event dispatch thread's queue. The event dispatch thread will then pull the event, determine which component(s) need to be notified (for example by checking the click's coordinates) and call the corresponding methods on the components.

    You can find more information about thread in Swing here: http://docs.oracle.com/javase/tutorial/uiswing/concurrency/

    The approach described above applies more or less to most GUI frameworks.