If you want to run a long time job in non-UI thread and keep the UI responsive, but is other is locked(can refreshes, but don't in accept interaction), you can use the following two ways:
// First Way
ModalContext.setAllowReadAndDispatch(false); //false means the job will run in UI thread, otherwise the progress dialog will pop up
IProgressService progressService = PlatformUI.getWorkbench().getProgressService();
Assert.isNotNull(progressService);
try{
// this will set the cursor as busy state
progressService.busyCursorWhile(new IRunnableWithProgress() {
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedExection{
try{
Thread.sleep(1000);
System.out.println("This is the long time process");
} catch(java.lang.InterruptedException e1){
// LOG.logEror("TODO", e1);
}
}
});
} catch(InvocationTargetException | InterruptedException e) {
System.out.println("Catch Exception");
} finally {
ModalContext.setAllowReadAndDispatch(true);
}
// Second Way
ModalContext.setAllowReadAndDispatch(false); //false means the job will run in UI thread, otherwise the progress dialog will pop up
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
Assert.isNotNull(progressService);
try{
window.run(true, true, new IRunnableWithProgress() {
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedExection{
try{
Thread.sleep(1000);
System.out.println("This is the long time process");
} catch(java.lang.InterruptedException e1){
// LOG.logEror("TODO", e1);
}
}
});
} catch(InvocationTargetException | InterruptedException e) {
System.out.println("Catch Exception");
} finally {
ModalContext.setAllowReadAndDispatch(true);
}
e.g. If you click a button to trigger a time-consuming query action, then the UI will hang up. After use below way, you can move the mouse and click other button the UI won't be hang up any more.