Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Plugin Development Environment (PDE) » Questions about UI/non-UI thread in an eclipse plugin
Questions about UI/non-UI thread in an eclipse plugin [message #754167] Tue, 01 November 2011 03:19 Go to next message
Sai Missing name is currently offline Sai Missing name
Messages: 25
Registered: March 2011
Junior Member
Hi all-

I often encounter the annoying "SWTException: invalid thread access" exception when writing my plugins or using others' plugins.

As far as I understood, this error occurs when UI operations are performed by a non-UI element, without wrapping that inside "Display.asyncExec/syncExec" invocation. If I understood it in a wrong way, please correct me. Thank you.

To better understand why this problem happens (and how to avoid it), I got 1 simple question:

where is the UI/non-UI threads in an eclipse plugin? Normally, can I assume that there is only 1 UI thread in an eclipse plugin? If so, are all classes for the extension points are directly invoked by the UI-thread? more specially, I can specify to extend the "org.eclipse.ui.views" using my own class "plugintest.views.SampleView" as follows. So, is "SampleView" initialized and executed by a UI-thread? More generally, is this true for all UI extensions?

<extension
point="org.eclipse.ui.views">
<category
name="Sample Category"
id="plugintest">
</category>
<view
class="plugintest.views.SampleView"
id="plugintest.views.SampleView">
</view>
</extension>


Thank you very much, any comment is highly appreciated.

-Sai
Re: Questions about UI/non-UI thread in an eclipse plugin [message #754261 is a reply to message #754167] Tue, 01 November 2011 12:55 Go to previous messageGo to next message
Curtis Windatt is currently offline Curtis Windatt
Messages: 166
Registered: July 2009
Senior Member
There is only one UI thread. Whether a given extension point calls your code from the UI thread or not is specific to the extension point. Most will not use the UI thread as doing so will make Eclipse unresponsive while it waits for client code to run.

I do not know if the view creation is done in the UI thread, but the init() and other calls driven by the UI should be.
Re: Questions about UI/non-UI thread in an eclipse plugin [message #754265 is a reply to message #754261] Tue, 01 November 2011 13:07 Go to previous messageGo to next message
Sai Missing name is currently offline Sai Missing name
Messages: 25
Registered: March 2011
Junior Member
Thank you very much, Curtis.

Two simple follow-on question:

1. do you know where I find (or which eclipse documentation I can refer to) whether an extension point is invoked by UI thread? It would be great if you can point me to a list of extension points that are for sure invoked by UI thread. Thanks.

2. I would like to understand in deep on how plugin is invoked by eclipse. Does eclipse invoke each plugin by reflection (according to what has specified in the plugin.xml file)? In so, could you please kindly guide me to some reference on how an eclipse plugin is invoked?

P.S., Since I get bored by the annoying UI thread access error, I am now planning to write a tool which can automatically detect such related errors, and contribute to the eclipse community (if the precision is acceptable). So, I would like to get a deeper understanding in how eclipse code interacts with plugin code.

Thanks a lot, any suggestion is welcome.

-Sai


Curtis Windatt wrote on Tue, 01 November 2011 12:55
There is only one UI thread. Whether a given extension point calls your code from the UI thread or not is specific to the extension point. Most will not use the UI thread as doing so will make Eclipse unresponsive while it waits for client code to run.

I do not know if the view creation is done in the UI thread, but the init() and other calls driven by the UI should be.

Re: Questions about UI/non-UI thread in an eclipse plugin [message #754281 is a reply to message #754265] Tue, 01 November 2011 15:09 Go to previous messageGo to next message
Curtis Windatt is currently offline Curtis Windatt
Messages: 166
Registered: July 2009
Senior Member
For more information on the UI thread, try:
http://www.eclipse.org/swt/faq.php#uithread

There is no guarantee that API will document what thread it will be called from. There are some listeners in the platform that specify whether or not they will be called from a separate thread or the UI thread. Any documentation will likely be in the javadoc of the interface being implemented or the method used to contribute. The extension point schema (.exsd) may also have documentation.

Most plug-ins access contributed extensions using the platform's registry, see Platform.getExtensionRegistry(). With the registry you can collect all contributors to an extension point, read their attributes and create executable extensions (for extensions specifying a java class).
Re: Questions about UI/non-UI thread in an eclipse plugin [message #754294 is a reply to message #754281] Tue, 01 November 2011 17:12 Go to previous messageGo to next message
Sai Missing name is currently offline Sai Missing name
Messages: 25
Registered: March 2011
Junior Member
Thank you very much, Curtis. The information your provided is very helpful.

A few more simple questions:

1. in eclipse framework, all UI extension points must be called by UI-thread, right? otherwise, there will be an error in creating the extended views. For example, we can extend the ui.view with our own SampleView class like:

<extension
point="org.eclipse.ui.views">
<category
name="Sample Category"
id="plugintest">
</category>
<view
name="Sample View"
...
class="plugintest.views.SampleView"
id="plugintest.views.SampleView">
</view>
</extension>

So, the above plugintest.views.SampleView class must be created and invoked by a UI-thread, right? If so, I think I may follow call relations from all public methods from the UI class to check if such methods will spawn new threads (non-UI thread) that perform UI operations, and lead to invalid thread access errors. I am not sure whether this assumption holds or not.

2. Does "Platform#getExtensionRegistry()" mean "org.eclipse.core.runtime.Platform#getExtensionRegistory()"? Using that method, I could get an "ExtensionRegistry" object. However, how can I acquire information on contributors to an extension point, and their attributes? Can you please give me some guidance along this line?

Thanks a lot, really appreciate your time and answer.

-Sai


Curtis Windatt wrote on Tue, 01 November 2011 15:09
For more information on the UI thread, try:
http://www.eclipse.org/swt/faq.php#uithread

There is no guarantee that API will document what thread it will be called from. There are some listeners in the platform that specify whether or not they will be called from a separate thread or the UI thread. Any documentation will likely be in the javadoc of the interface being implemented or the method used to contribute. The extension point schema (.exsd) may also have documentation.

Most plug-ins access contributed extensions using the platform's registry, see Platform.getExtensionRegistry(). With the registry you can collect all contributors to an extension point, read their attributes and create executable extensions (for extensions specifying a java class).

Re: Questions about UI/non-UI thread in an eclipse plugin [message #754442 is a reply to message #754294] Wed, 02 November 2011 10:39 Go to previous messageGo to next message
Curtis Windatt is currently offline Curtis Windatt
Messages: 166
Registered: July 2009
Senior Member
1) I don't think the creation of SampleView must be created from a UI thread (though it probably is). Often the constructor is not expected to make any UI elements. The init call or whatever that passes in a context (Control, ViewPart, etc.) will be called from the UI thread.

2) Here is some code from PDE where we load an extension contributing a wizard:

IExtensionRegistry registry = Platform.getExtensionRegistry();
IExtensionPoint point = registry.getExtensionPoint(PDEPlugin.getPluginId(), TARGET_PROVISIONER_POINT);
if (point == null)
return list;
IExtension[] extensions = point.getExtensions();
for (int i = 0; i < extensions.length; i++) {
IConfigurationElement[] elements = extensions[i].getConfigurationElements();
for (int j = 0; j < elements.length; j++) {
WizardElement element = createWizardElement(elements[j]);
if (element != null) {
final String pluginId = element.getPluginId();
final String contributionId = element.getID();
Re: Questions about UI/non-UI thread in an eclipse plugin [message #754489 is a reply to message #754442] Wed, 02 November 2011 13:53 Go to previous message
Sai Missing name is currently offline Sai Missing name
Messages: 25
Registered: March 2011
Junior Member
Thanks a lot, Curtis, for your quick reply.


>I don't think > the creation of SampleView must be created from a UI thread (though it probably
> is). Often the constructor is not expected to make any UI elements. The init call
> or whatever that passes in a context (Control, ViewPart, etc.) will be called from
>the UI thread.

In other words, after the SampleView is created, its public methods should be called from a UI-thread, right? More precisely, how can eclipse creates a new SampleView object, and initilaize its state for receiving upcoming events? Is there any code like:

SampleView view = new SampleView(); //or reflectively creation
view.initialize().... //or other setup method sequences


> 2) Here is some code from PDE where we load an extension contributing a wizard:
> IExtensionRegistry registry = Platform.getExtensionRegistry();
> IExtensionPoint point = >registry.getExtensionPoint(PDEPlugin.getPluginId(), TARGET_PROVISIONER_POINT);
> if (point == null)
> return list;>
> IExtension[] extensions = point.getExtensions();
> for (int i = 0; i < extensions.length; i++) {
> IConfigurationElement[] elements = >extensions[i].getConfigurationElements();
> for (int j = 0; j < elements.length; j++) {
> WizardElement element = >createWizardElement(elements[j]);
> if (element != null) {
> final String pluginId = >element.getPluginId();
> final String contributionId = >element.getID();[/quote]

Thank you for this example. May I ask, for example, how does eclipse set up environment to create the SampleView object?

<extension
point="org.eclipse.ui.views">
<category
name="Sample Category"
id="plugintest">
</category>
<view
name="Sample View"

In my understanding, eclipse will iterate all extension point, and find SampleView class, and then reflectively creates a SampleView object. Is that correct?

In addition, such as the event handling method below:

SampleView#selectionChanged
public void selectionChanged(IAction action, ISelection selection) {
}

when there is changed selection event, I guess the eclipse framework creates an IAction and ISelection object, and calls back the selectionChanged. I am a bit curious to understand how eclipse interacts with its loaded plugins, and would like to know the eclipse code that performs the above operations.

Thanks a lot. Really appreciate your help.

-Sai

[Updated on: Wed, 02 November 2011 14:57]

Report message to a moderator

Previous Topic:launching external app when plugin button is pressed
Next Topic:Generate Eclipse Product to Linux from Windows
Goto Forum:
  


Current Time: Wed Apr 16 00:41:02 EDT 2014

Powered by FUDForum. Page generated in 0.01907 seconds