How to complete a task when a user closes an Eclipse application

I am using org.eclipse.core.runtime.jobs.Job to execute a stored procedure that deletes data and updates the user interface to match the new data. Therefore, it is important that this work is completed even if the user closes the eclipse application.

final Job execStoredProcJob = new Job(taskName) { protected IStatus run(IProgressMonitor monitor) { monitor.beginTask(taskName, // execute stored procedure // update user interface monitor.done(); return Status.OK_STATUS; } }; execStoredProcJob.schedule(); 

When I close the eclipse application while Job is running, it seems to kill Job. How to shut down after a user closes an eclipse application? Is it possible?

+4
source share
2 answers

I think you can take a look at the planning rules http://www.eclipse.org/articles/Article-Concurrency/jobs-api.html

 execStoredProcJob.setRule([Workspace root]); execStoredProcJob.schedule(); 

[Workspace root] can be obtained as project.getWorkspace().getRoot() if you have a link to your project.

This will block all jobs requiring the same rule. Shutting down is one of them.

It is also possible:

 IWorkspace myWorkspace = org.eclipse.core.resources.ResourcesPlugin.getWorkspace(); 

Then use:

 myWorkspace.getRoot(); 
+1
source

An alternative to planning rules is to add code to WorkbenchAdvisor.preShutdown() , which will join any outstanding job that you have ...

0
source

Source: https://habr.com/ru/post/1392863/


All Articles