SWT problem with syncExec ()

This is my first question about StackOverflow (sorry for my english). I will try to explain the problem as I can.

I have a swt application with a front jade application in which I have a progress bar to inform about the duration of the application. To update this progress bar, I use:

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

Progress:

 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 am trying to analyze the time this Runnable takes and it is constant, but when I parse this line sSehll.getDisplay().syncExec(progress) takes different times (from 0ms to XXXXms)

I read it

syncExec (Runnable runnable) calls the current thread (if it is different from the user interface thread on the display) to wait for the runnable to complete.

But runnable is constant over time ...

Can anyone guide me? I don’t understand why it sometimes takes 3 minutes at another time.

thanks

+7
java swt agents-jade
source share
2 answers

There are two methods in the SWT Display class, syncExec and aSyncExec. They are used when you have a thread that is not a user interface thread that somehow wants to update the interface. Basically, when you name them, you basically tell the user interface thread that you have something that you want to do when it has a chance. In this way, the UI thread will continue its current work, and at some point it will do what you requested. When he does this, it will always change depending on what other work the user interface thread should do at this time.

The difference between aSyncExec and syncExec is whether the code calling it will wait for the execution to complete. If you call aSyncExec, the next statement in the calling thread will execute immediately. If you call syncExec, then your calling thread will sit and wait for the user interface thread to actually execute the code and return. By the time, how much time is required to execute syncExec, you therefore cannot determine how much time is required to execute the execution method, but also how long before the user interface thread starts to run it.

Do not succumb to replacing syncExec with aSyncExec. If you take the time to run aSyncExec, you will find it even faster. But this is because all you need is the time it takes to tell the user interface thread that you have something for it (not how long it takes to complete it).

+3
source share

According to Display class documentation , syncExec :

Calls the run () method which can be invoked by the user interface thread at the next reasonable opportunity . The thread that calls this method is paused until execution is complete. Specifying a null value as runnable just wakes the user interface thread.

Therefore, your run method runs in constant time, but the Display object may not always invoke it immediately.

+1
source share

All Articles