|
Re: Eclipse Job custom ProgressMonitorDialog instance [message #1718770 is a reply to message #1718762] |
Wed, 30 December 2015 03:12 |
|
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 |
Ed Merks Messages: 33216 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 #1718852 is a reply to message #1718841] |
Thu, 31 December 2015 07:13 |
Ed Merks Messages: 33216 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/
|
|
|
Powered by
FUDForum. Page generated in 0.08064 seconds