Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » Eclipse Job custom ProgressMonitorDialog instance
Eclipse Job custom ProgressMonitorDialog instance [message #1718762] Tue, 29 December 2015 22:23 Go to next message
Simon Eismann is currently offline Simon EismannFriend
Messages: 5
Registered: August 2015
Junior Member
Hello,

I have the following problem:
I am starting my script as an eclipse job (org.eclipse.core.runtime.jobs.Job). I now want the job to cancel immediatly (interupt/stop the thread) when the user clicks on the cancel button. This is important to me, since i have very time intensive calls to external APIs which means the normal approach of polling the monitor.isCancelled() flag is not really applicable for me. My idea here is to run the job with a custom ProgressMonitorDialog, that overrides the cancelPressed() method. But how do i start a job with a custom ProgressMonitorDialog? Or is there an easier approach?

Greetings,
Simon

PS: I have no idea where this post belongs, i hope this isn't tooo wrong Razz

Re: Eclipse Job custom ProgressMonitorDialog instance [message #1718770 is a reply to message #1718762] Wed, 30 December 2015 03:12 Go to previous messageGo to next message
Russell Bateman is currently offline Russell BatemanFriend
Messages: 3798
Registered: July 2009
Location: Provo, Utah, USA
Senior Member

On 12/29/2015 03:23 PM, Simon Eismann wrote:
> Hello,
>
> I have the following problem:
> I am starting my script as an eclipse job
> (org.eclipse.core.runtime.jobs.Job). I now want the job to cancel
> immediatly (interupt/stop the thread) when the user clicks on the cancel
> button. This is important to me, since i have very time intensive calls
> to external APIs which means the normal approach of polling the
> monitor.isCancelled() flag is not really applicable for me. My idea here
> is to run the job with a custom ProgressMonitorDialog, that overrides
> the cancelPressed() method. But how do i start a job with a custom
> ProgressMonitorDialog? Or is there an easier approach?
>
> Greetings,
> Simon
>
> PS: I have no idea where this post belongs, i hope this isn't tooo wrong :p

Simon,

This is the place. Someone will be along shortly.

Cheers
Re: Eclipse Job custom ProgressMonitorDialog instance [message #1718774 is a reply to message #1718762] Wed, 30 December 2015 05:23 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
Simon,

Perhaps an easier approach would be to have your job's run method create
a thread that polls the monitor's isCancelled state, with short
Thread.sleeps to keep it waiting relatively non-busy. When it detects
that isCancelled becomes true, you can do what you need to do to
interrupt the job's thread. You'll want to be sure to stop such a
thread if the job completes normally.


On 29/12/2015 11:23 PM, Simon Eismann wrote:
> Hello,
>
> I have the following problem:
> I am starting my script as an eclipse job
> (org.eclipse.core.runtime.jobs.Job). I now want the job to cancel
> immediatly (interupt/stop the thread) when the user clicks on the
> cancel button. This is important to me, since i have very time
> intensive calls to external APIs which means the normal approach of
> polling the monitor.isCancelled() flag is not really applicable for
> me. My idea here is to run the job with a custom
> ProgressMonitorDialog, that overrides the cancelPressed() method. But
> how do i start a job with a custom ProgressMonitorDialog? Or is there
> an easier approach?
>
> Greetings,
> Simon
>
> PS: I have no idea where this post belongs, i hope this isn't tooo
> wrong :p
>
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Eclipse Job custom ProgressMonitorDialog instance [message #1718841 is a reply to message #1718774] Thu, 31 December 2015 01:49 Go to previous messageGo to next message
Simon Eismann is currently offline Simon EismannFriend
Messages: 5
Registered: August 2015
Junior Member
Hey Ed,

Your solution worked beautifully. My setup looks like this now:

public class CancelableEngineJob extends Job {
	private boolean done = false;

	@Override
	protected IStatus run(IProgressMonitor monitor) {
        Thread t = new Thread(new WorkThread(monitor, this));
		done = false;
        
        t.start();
        
        while (!monitor.isCanceled() && !done) {
        	try { Thread.sleep(500); } catch (InterruptedException e) {}
        }
        
		return Status.OK_STATUS;
	}
	
	public void notifiyWorkIsDone() {
		done = true;
	}

}


and

class WorkThread implements Runnable {
	private IProgressMonitor monitor;
	private CancelableEngineJob job;

	public WorkThread(IProgressMonitor monitor, CancelableEngineJob job) {
		this.monitor = monitor;
		this.resource = resource;
		this.job = job;
	}

	@Override
	public void run() {
		... do api cals

		notifyAboutFinish(listeners);
	}

	private void notifyAboutFinish() {
		monitor.done();
		job.notifiyWorkIsDone();
	}
}



Which creates lightningfast responses to the cancel button, no matter what i do inside the workthread Smile Thank you for your help
Re: Eclipse Job custom ProgressMonitorDialog instance [message #1718852 is a reply to message #1718841] Thu, 31 December 2015 07:13 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
Simon,

It's very nice of you to share your implementation! Happy New Year. :-)

On 31/12/2015 2:49 AM, Simon Eismann wrote:
> Hey Ed,
>
> Your solution worked beautifully. My setup looks like this now:
>
>
> public class CancelableEngineJob extends Job {
> private boolean done = false;
>
> @Override
> protected IStatus run(IProgressMonitor monitor) {
> Thread t = new Thread(new WorkThread(monitor, this));
> done = false;
> t.start();
> while (!monitor.isCanceled() && !done) {
> try { Thread.sleep(500); } catch (InterruptedException e) {}
> }
> return Status.OK_STATUS;
> }
>
> public void notifiyWorkIsDone() {
> done = true;
> }
>
> }
>
>
> and
>
> class WorkThread implements Runnable {
> private IProgressMonitor monitor;
> private CancelableEngineJob job;
>
> public WorkThread(IProgressMonitor monitor, CancelableEngineJob
> job) {
> this.monitor = monitor;
> this.resource = resource;
> this.job = job;
> }
>
> @Override
> public void run() {
> ... do api cals
>
> notifyAboutFinish(listeners);
> }
>
> private void notifyAboutFinish() {
> monitor.done();
> job.notifiyWorkIsDone();
> }
> }
>
>
>
> Which creates lightningfast responses to the cancel button, no matter
> what i do inside the workthread :) Thank you for your help


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:Can't install any plugins
Next Topic:comment on the same line as code
Goto Forum:
  


Current Time: Thu Mar 28 22:32:55 GMT 2024

Powered by FUDForum. Page generated in 0.04742 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top