Java – SWT problem using syncexec()

This is my first question on stackoverflow I'll try my best to explain the problem

I have a SWT application with 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 spent by this runnable and it is constant, but when I analyze this line, ssehll getDisplay(). Syncexec (Progress) takes different time (from 0ms to xxxxms)

I've read this

But runnable is a time constant

Can someone guide me? I don't understand why it sometimes takes three minutes and other time

thank you

Solution

There are two methods in the SWT display class, syncexec and assyncexec When your threads are not UI threads that want to update the UI in some way, they are used Basically, when you call them, you basically say to the UI thread what you want it to do when you have a chance Therefore, the UI thread will continue its current work, and at some point it will perform what you ask it to do When it happens, it will always be different, depending on what else the UI thread must do at that time

The difference between syncexec and syncexec is whether the code calling it waits for execution to complete If asyncexec is called, the next statement in the calling thread will be executed immediately If you call syncexec, your calling thread will wait until the UI thread actually executes the code and returns By calculating the time required to execute syncexec, you can calculate not only the execution time of the running method, but also the time when the UI thread starts running it

Do not attempt to exchange syncexec with asyncexec here If you spend how long asyncexec executes, you'll find it faster But that's because all your time is the time it takes to tell the UI thread how much you need to do (it doesn't take long)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>