Question about UI / non-UI listeners [message #780632] |
Wed, 18 January 2012 20:13 |
Sai Missing name Messages: 25 Registered: March 2011 |
Junior Member |
|
|
Hi everyone:
I wrote an eclipse plugin, and got many "invalid thread errors". I know that this error is caused by "accessing a GUI object from a non-UI thread", but am *really* confused on the huge volume of "EventListener"s sublclasses in eclipse. I was surprised to find some listeners are called from UI-thread, while others are called from non-UI thread.
For example,
AWTEventListener, IResourceChangeListener and IJobChangeListener all implement the EventListener interface. However, I found the call back method in AWTEventListener is always called by UI-thread, thus the object access code there do not need to be wrapped by sync/asyncExec. But the other two listeners (IResourceChangeListener, and IJobChangeListener) are often called by non-UI thread, if the call back there access some UI object, an invalid thread error becomes apparent.
My question is "which listeners will be called by UI/non-UI thread"? is there any rule for this? If not, do I need to remember (hardcode) those facts?
[I got some answers as follows:
If your non-UI based listener needs to call UI object, this call should happen using asyncExec or syncExec.
For the bigger part of your question, you may get a better answer in the 'Eclipse Platform' forums.
]
I am really interested in knowing what would be the UI/non-UI based listeners in Eclipse's platform.
thanks a lot, any suggestion is highly appreciated.
-Sai
|
|
|
Re: Question about UI / non-UI listeners [message #782811 is a reply to message #780632] |
Tue, 24 January 2012 14:02 |
Eclipse User |
|
|
|
I think you may be wrongly lumping AWT/Swing together with SWT -- they are distinct and cannot be mixed. SWT is completely independent of AWT and does not share any code. They each have their own UI thread.
Although SWT and AWT/Swing can co-exist (search for the SWT_AWT bridge), there are some caveats (search for Gordon Hirsch's article, though it's a bit out of date). You have to make sure you use the right UI thread, either through Display.asyncExec() for SWT or SwingUtilities.invokeLater() for AWT/Swing. If you are having to bridge between the SWT and AWT/Swing, then avoid the synchronous versions as it's a sure source of deadlocks.
|
|
|
|
Powered by
FUDForum. Page generated in 0.04217 seconds