开发者

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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜