Apparently all Eclipse/SWT has in the way of managing the busy mouse indicator is
BusyIndicator.showWhile(Runnable synchronousStuffToDo)
However, I have a fundamentally event-oriented project where "stuff to do" doesn't happen within a sequential line of execution: an action gets ordered and a continuation-callback is provided to the execution manager. Therefore I have nothing meaningful to put into that synchronousStuffToDo runnable.
Is there another, however low-level and clumsy, but platform-independent way of manipulating the busy indicator asynchronously, which means two separate method calls, "activate it" and "deactivate it"?
I should add ProgressMonitorDialog to this question because it appears to suffer from the same problem. Yes, within the ProgressMonitorDialog#run method an inner event loop will be spinned, but SWT event loop is just one of my execution managers, so the chain will still be broken. Apparently without this class I can't even show a progress monitor except if I reimplement from lower-level primitives.
There is no way you can manipulate the Cursor using the BusyIndicator class.
You can invoke the below util method to show a Busy Icon while running your job on a background Thread
public static void imBusy(final boolean busy){
Display.getDefault().asyncExec(new Runnable()
{
@Override
public void run()
{
Shell shell = Display.getDefault().getActiveShell();
if(busy){ //show Busy Cursor
Cursor cursor = Display.getDefault().getSystemCursor(SWT.CURSOR_WAIT);
shell.setCursor(cursor);
}else{
shell.setCursor(null);
}
}
});
}
11 years later (and using a more recent version of Java), you might consider, using Eclipse 4.33 (Q3 2024):
Enhanced BusyIndicator API for Futures
The
BusyIndicatorclass defines the following new method:
static void showWhile(Future> future)If called from a
Displaythread, it waits for the givenFutureto complete and provides busy feedback using the busy indicator.While waiting for completion, pending UI events are processed to prevent UI freeze.
If there is no Display for the current thread,
Future.get()will be called, ignoring anyExecutionExceptionand no busy feedback will be displayed.
In your case:
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
// assynchronousStuffToDo
});
BusyIndicator.showWhile(future);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With