Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynchronously control the BusyIndicator

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.

like image 973
Marko Topolnik Avatar asked Nov 24 '25 01:11

Marko Topolnik


2 Answers

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);
            }   
        }
    });

}
like image 72
Niranjan Avatar answered Nov 26 '25 15:11

Niranjan


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 BusyIndicator class defines the following new method:

 static void showWhile(Future> future)

If called from a Display thread, it waits for the given Future to 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 any ExecutionException and no busy feedback will be displayed.

In your case:

CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
    // assynchronousStuffToDo 
});

BusyIndicator.showWhile(future);
like image 30
VonC Avatar answered Nov 26 '25 14:11

VonC