Kasper
Kasper

Reputation: 690

Progressbar not update in time

I have a problem. On my (eclipse 4 based) GUI I have two objects:

Button button = new Button(groupInitialize, SWT.NONE);
ProgressBar bar= new ProgressBar(group, SWT.SMOOTH);

I set a listner for the button so that any time that the button is pressed an elaboration begin. During this elaboration the status bar has to be updated.

// Button listener definition!
button .addListener(SWT.Selection, new Listener() {
    @Override
    public void handleEvent(final Event event) {
        Runnable run = new Runnable() {
            @Override
            public void run() {
                Display display = PlatformUI.getWorkbench().getDisplay();
                display.asyncExec(new Runnable() {
                    public void run() {
                        myLongLastingMethod();
                    }
                });

            }
        };
        new Thread(run).start();
    }
});

and this is what I do in the myLongLastingMethod():

private void myLongLastingMethod() {
    action1();
    update();

    action2();
    update();

    action3();
    update();
}

And finally the update method (which -should- update the progress bar):

UPDATED

private void update() {
    if (progressBar.isDisposed()) {
        return;
    }
    int selection = progressBar.getSelection();
    progressBar.setSelection(++selection);
}

I'm sure I'm doing something wrong ... Any idea/help why it shouldn't work?

Upvotes: 0

Views: 318

Answers (1)

greg-449
greg-449

Reputation: 111216

Your background thread Runnable is using display.asyncExec to call your long running method. display.asyncExec runs the code in the User Interface thread, so nothing else will happen in the UI while that code is running. You should only use asyncExec to run short code which updates the UI.

Just call myLongLastingMethod directly in the background thread without the asyncExec call.

So:

public void handleEvent(final Event event) {
    Runnable run = new Runnable() {
        @Override
        public void run() {
           myLongLastingMethod();
        }
    };
    new Thread(run).start();
}

Upvotes: 1

Related Questions