English 中文(简体)
Best practice for passing values from a non-UI thread to a UI thread in an Eclipse plugin application
原标题:

Is there a best practice/shining example out there of passing values from a non-UI thread to a UI thread in an Eclipse plugin application?

问题回答

You could use Display.asyncExec()

To allow background threads to perform operations on objects belonging to the UI-thread, the methods syncExec(Runnable runnable) and asyncExec(Runnable runnable) of Display are used.
These are the only methods in SWT that can be called from any thread.
They allow a runnable to be executed by the UI-thread, - either synchronously, causing the background thread to wait for the runnable to finish, - or asynchronously allowing the background thread to continue execution without waiting for the result.

A runnable that is executed using syncExec() most closely matches the equivalent direct call to the UI operation because a Java method call always waits for the result before proceeding, just like syncExec().

As illustrated by this thread:

I thought all those runnables or threads I give to Display.sync or asyncExec are Threads and they get scheduled by the jvm or something along with the UI thread!
I never knew they are not considered the threads, but only pieces of code executed asynchronously by the UI thread!

This piece of code asynchronously executed by the UI thread might be a good place to access values (synchronized access) from other thread.

See "How to update a GUI from another thread in Java" as a practical example of passing a value to the UI thread.

(Note: the non-eclipse non-SWT way would have been, in Swing, by using a Swing Worker, as I mentioned a year ago)

A little late getting into this one, but I would use a org.eclipse.ui.progress.UIJob.

When your non-UI thread has some information to send to the UI, it can spawn off a UIJob, that runs in a particular Display. Behind the scenes, asyncExec is used, but you get a lot of nice parts of the Jobs API along with it like a ProgressMonitor, job cancelation, rule scheduling, and join/waiting for other jobs.





相关问题
How to start to create an application GUI using C#?

HI! I am new to C# and plan to use it for my application GUI. I am trying to make my GUI similar to SPSS:http://www.spss.com/images/08/statistics_screens/ez_rfm-big.jpg Is this easy in C#? Is there ...

Automatic height of edit box

My shoes application has three items stacked on top of each other (with a stack, of course), in order: A banner An edit box Two buttons in a flow What I want to do is have the banner stay at it s ...

Search by using the keyboard in a list/grid - algorithm

I need to implement a custom search in a grid and I would like to find some user interface guidelines that explain the standard way to implement it. I mean this kind of search that is initiated by ...

UI And TcpClient Issue in vb.net

I m having some problems with a small ircbot i m writing. Basically I connect to the server using a tcpclient in a seperate class, which also runs on its own thread. I want to display the server text ...

UI Convention: Shortcut key for application exit? [closed]

Is there a convention for the shortcut keys for application exit? Some applications uses Alt+X some others use Ctrl+ X and Ctrl+Q. Applications like FF and IE doesnot assign a shortcut at all. So is ...

热门标签