Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SWT issue with syncExec()

This is my first question on StackOverflow (sorry about my english). I'll try to explain the problem as well I can.

I have a swt application with a foreground jade application in which I have a progress bar to inform the duration of the application. To refresh this progress bar I use:

if(Display.getCurrent() != null) {
    progress.run();
}
else {
   sShell.getDisplay().syncExec(progress);
}

Progress is:

Runnable progress = new Runnable() {
    public void run () {
        if (progressBar.isDisposed ()) 
            return;
        int percentage= (numStep*100)/maxSteps;
        progressBar.setSelection(percentage);
        if (numStep >= maxSteps){
            label1.setText("The simulation has been completed.");
            button.setEnabled(true);
        }    
    }
};

I try to analyze the time that this Runnable takes and it is constant, but when I analyze this line sSehll.getDisplay().syncExec(progress) takes different times (from 0ms to XXXXms)

I have read this

syncExec(Runnable runnable) causes the current thread (if it is different than the user interface thread of the display) to wait for the runnable to finish.

But the Runnable is time constant...

Can someone guide me? I don't understand why sometimes takes 3 minutes and some other time.

Thanks

like image 517
Michel Avatar asked Mar 04 '10 13:03

Michel


1 Answers

There are two methods in SWT Display class, syncExec and aSyncExec. They are both used when you have a thread that is not the UI thread that wants to update the UI in some way. Basically when you call them you are basically saying to the UI thread that you have something you want it to do when it gets a chance. So the UI thread will continue its current work and at some point it will do what you asked it to do. When it does it will always vary depending on what other work the UI thread has to do at that time.

The difference between aSyncExec and syncExec is whether or not the code that calls it waits for the execution to finish. If you call aSyncExec then the execution of the next statement in your calling thread will execute immediately. If you call syncExec then your calling thread will sit and wait until the UI Thread actually executes the code and returns. By timing how long it takes to execute syncExec you are therefore timing not just how long it takes the run method to execute, but also how long before the UI thread actaully starts running it.

Don't be tempted to swap syncExec with aSyncExec here. If you time how long it takes aSyncExec to execute you will find it is even faster. But that is because all you are timing is how long it takes to tell the UI thread that you have something for it to do (not how long it takes to do it).

like image 60
DaveJohnston Avatar answered Sep 22 '22 14:09

DaveJohnston