I have situation like, once copying of items is completed it has to change the ui. I have created a job to copy the items and joined job so that until its completed it will not modify the ui. But ui is getting hanged until copying operation is completed. Here is my code
private void copyItems(SelectDTO selection, TreeViewer viewer) {
try {
Job job = new Job("test") {
@Override
protected IStatus run(IProgressMonitor monitor) {
ISafeRunnable runnable = new ISafeRunnable() {
@Override
public void run() throws Exception {
//copy items
}
@Override
public void handleException(Throwable exception) {
}
};
SafeRunner.run(runnable);
return Status.OK_STATUS;
}
@Override
public boolean belongsTo(Object family) {
}
};
I have joined job in addJobChangeListener
job.addJobChangeListener(new IJobChangeListener() {
@Override
public void sleeping(IJobChangeEvent event) {
// TODO Auto-generated method stub
}
@Override
public void scheduled(IJobChangeEvent event) {
// TODO Auto-generated method stub
}
@Override
public void running(IJobChangeEvent event) {
// TODO Auto-generated method stub
}
@Override
public void done(IJobChangeEvent event) {
}
@Override
public void awake(IJobChangeEvent event) {
// TODO Auto-generated method stub
}
@Override
public void aboutToRun(IJobChangeEvent event) {
// TODO Auto-generated method stub
}
});
job.schedule();
job.join();
} catch (Exception e) {
}
}
After calling this method i am updating the ui. What changes i can do to stop hanging of ui? Thank you.
Don't call join
like that. You are scheduling the job and then immediately waiting for it to finish which will block the UI until the jobs ends.
You can add code to the done
method of the job change listener to do processing when the job has finished.
So something like:
job.addJobChangeListener(new JobChangeAdapter()
{
@Override
public void done(IJobChangeEvent event)
{
// TODO Your work here
}
});
job.schedule();
I have used JobChangeAdapter
here rather than IJobChangeListener
directly as it provides default implementations of the methods you don't need to use.
An alternative is to use Display.asyncExec
in the Job
code to run some code in the UI thread.