Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Progressbar display sync
Progressbar display sync [message #1710881] Sat, 10 October 2015 08:30
Peter Vaclavek is currently offline Peter VaclavekFriend
Messages: 3
Registered: October 2015
Junior Member
Hi all!

I'm having a problem with continuous refreshing of progress bar (increment a progress bar until is full and do it in loop).
My goal is to fill my progress bar smoothly (in aprox. 5sec from 0 to max) and repeat this in a loop.

Here is snippet of my code, it's working but when progress is about 80% it will be suddenly reset to 0 (which is OK, but it should be done on 100%).
I don't understand this behavior. Even if I use the syncExec (thanks to which it should execute all the UI runnables until it continues further), the UI runnables were executed but UI was not updated (at least not in realtime but with delay)... it looks like every update of progress bar status is delayed on UI and when the main thread resets the progress bar back to 0 the UI is not displaying the actual state (which is really 100% in non UI thread) but it is only on 80% (because of some sort of paint delay).

How should be fixed? What is wrong with this code?

public class Test {

	private static boolean full = false;

	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setSize(200, 100);
		shell.open();

		ProgressBar bar = new ProgressBar(shell, SWT.SMOOTH);
		bar.setBounds(30, 10, 100, 30);
		bar.setMinimum(0);
		bar.setMaximum(50);

		new Thread(new Runnable() {

			@Override
			public void run() {

				// For ever and ever...
				while (true) {
					// Update UI & set progress bar to 0
					Display.getDefault().asyncExec(new Runnable() {

						@Override
						public void run() {
							// Some additional UI updates ...

							full = false;
							bar.setSelection(0);
						}
					});

					// Until progress bar isn't full...
					while (!full) {

						// Sleep
						try {
							Thread.sleep(100);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}

						// Increment progress bar
						Display.getDefault().syncExec(new Runnable() {
							@Override
							public void run() {
								if (bar.getSelection() >= bar.getMaximum()) {
									full = true;
									return;
								}

								bar.setSelection(bar.getSelection() + 1);
							}
						});
					}
				}

			}
		}).start();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
}

[Updated on: Mon, 12 October 2015 19:58]

Report message to a moderator

Previous Topic:font display issues with mars/centos6
Next Topic:Shortcut key for combobox?
Goto Forum:
  


Current Time: Sat Dec 16 09:21:37 GMT 2017

Powered by FUDForum. Page generated in 0.02105 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software