I understand that in .NET, one needs to use Control.Invoke(delegate) to perform operations on a control. This lead me to wondering in which environments Invoke is actually required. As far as i know, it was not required in older versions of, say, Visual Basic and Pascal. In particular, what is the status of Java (possibly version-dependent?) and ‘old style’ windows GUI programming (manually reading the message queue)?
I understand that in .NET, one needs to use Control.Invoke(delegate) to perform operations on
Share
In most languages Invoke for GUI operations is necessary. In Java there is SwingUtilities.invokeLater. Actually a method like that would be necessary in any environment if more than one threads are used. The reason is that the main UI thread runs an event notification mechanism. A second thread to be able to interact with this mechanism needs somehow to be synchronized with it.
These invoke methods are actually a convenience for the developer. In platforms that do not exist, it doesn’t mean that is allowed to access UI items from a different thread. It may just means that the developer should implement a custom mechanism (sending events) to do so. UI code is very rarely thread safe. I have worked with many platforms and technologies and I am always following this rule: touch the UI from a single thread only.