tags:

views:

319

answers:

2

Hi, this is my first question on stackoverflow (sorry about my english). I'll try to explain the problem as well I can.

I have an 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

A: 

According to the Display class documentation, syncExec:

Causes the run() method of the runnable to be invoked by the user-interface thread at the next reasonable opportunity. The thread which calls this method is suspended until the runnable completes. Specifying null as the runnable simply wakes the user-interface thread.

Therefore, your run method is running in constant time, but the Display object may not always call it immediately.

Adam Paynter
I remember that when I read the documentation, but there is no way to force it?
Michel
And if you ever get syncExec to take three minutes then you probably have code executed in the SWT thread, which should be put into a background thread. Doing so will let the UI run smoothly again.
Christopher Oezbek
+2  A: 

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).

DaveJohnston
But there is no way to execute it inmediatly?
Michel
No. It should execute pretty quickly though, unless your UI thread is doing a lot of work (and if that is the case you may want to look at what it is doing and try to get that working in a different thread).If it is really taking 3 minutes then I guess your UI thread is doing something else? Is it UI related work it is doing?
DaveJohnston
Yes, it is related...
Michel
Then that work will have to stay in the main UI thread and any other request to this thread to do work will have to wait their turn.
DaveJohnston
I'll leave it as impossible
Michel