Difference Between Job and Thread? [message #552128] |
Tue, 10 August 2010 20:46  |
Eclipse User |
|
|
|
Hey all,
I'm developing a plug-in for Eclipse. I'm executing a long-running service invocation job over the network and for that, I'm using a thread that is supposed to work concurrently with the UI thread. Using IWorkbenchWindow.run(...) method I'm starting the thread and informing the user about the progress at the same time.
The problem is IWorkbenchWindow.run method is still using the UI thread and user can not interact with the GUI unless the thread is done.
So I googled and found out that since Eclipse 3.0 a Job API has been introduced. Can somebody please tell me the exact difference between a Job and a Thread?
Thanks a lot,
Bahar
|
|
|
|
|
|
|
|
|
|
|
|
|
Re: Difference Between Job and Thread? [message #559540 is a reply to message #559491] |
Fri, 17 September 2010 01:46  |
Eclipse User |
|
|
|
On 16.09.2010 21:42, Bahar Sateli wrote:
> Okay... again I'm answering my own question! 8)
>
> A little flash back:
> I had a job doing some service invocation over the net and bringing back
> some results that had to be shown in an eclipse view part instance.
> The invocation was right, the results were ready, but when I wanted to
> open the view... BAM! Invalid Thread Access thread was thrown.
>
> What was the problem? Simply because my job as it's been defined in
> Eclipse Job API is a Non-UI thread by nature, it didn't have access to
> the UI thread in order to use, for example, the Display.
>
> The solution was to make a hook to the UI thread. How? Like this:
>
> new Thread(new Runnable() {
>
> @Override
> public void run() {
>
> Display.getDefault().asyncExec(new Runnable() {
> @Override
> public void run() { // Do the UI stuff here. i.e. opening a view }
> });
>
> }
>
> }).start();
>
> I put this at the end of my job's run method. So when finished with the
> invocation, it can open the view after. Problem... solved! :)
Note that this "solution" is working by chance and is *not* the right
way to do that. Also there is an unnecessary overhead because of
creating a new thread that is absolutely not need. You just can call an
Display.asyncExec at any stage of your non-UI job where you think it's
OK to refresh the UI and if you want to ensure that your UI is
refreshed at the very end of your job, you just add a
IJobChangeListener listener for your job (your job can register for
that in it's own constructor, if you want) and during the
IJobChangeListener.done event you do the final Display.asyncExec.
HTH & Greetings from Bremen,
Daniel Krügler
|
|
|
Powered by
FUDForum. Page generated in 0.05550 seconds