Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Progressbar is frozen with syncExec
Progressbar is frozen with syncExec [message #1377185] Fri, 23 May 2014 19:23 Go to next message
mark tal is currently offline mark talFriend
Messages: 11
Registered: July 2012
Junior Member
I want to display a progres bar in GUI while a time consuming task, this task collects data from gui form, so I must use
 Display.getDefault().syncExec
or
Display.getDefault().asyncExec


in order to access GUI.

The issue with my following code, that while task is running the main GUI thread is locked and the progress bar is not "running".

Please help.

showProgress.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        try {
      
            new ProgressMonitorDialog(shell).run(true, true, new IRunnableWithProgress() {
				
	       public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
		  monitor.beginTask("Running long running operation", IProgressMonitor.UNKNOWN);
		  Display.getDefault().syncExec(new Runnable() {
		  public void run() {
		     for (int i = 0; i < 0100; i += 1) {
			try {
			    Thread.sleep(1000);								
			    readDataFromGUI();
			} catch (InterruptedException e) {								
			     e.printStackTrace();
			}

     		     }
		   }
          	});
		monitor.done();
	      }
	 });
        } catch (InvocationTargetException e) {
          MessageDialog.openError(shell, "Error", e.getMessage());
        } catch (InterruptedException e) {
          MessageDialog.openInformation(shell, "Cancelled", e.getMessage());
        }
      }
    });

[Updated on: Fri, 23 May 2014 19:51]

Report message to a moderator

Re: Progressbar is frozen with syncExec [message #1377350 is a reply to message #1377185] Fri, 23 May 2014 21:02 Go to previous messageGo to next message
David Wegener is currently offline David WegenerFriend
Messages: 1445
Registered: July 2009
Senior Member
The syncExec method calls the run method in the GUI thread so the GUI thread isn't locked, it is running your code. The syncExec (and asyncExec) calls exist to allow long running threads to update the display. They aren't there to execute long running code. The way they are intended to be used is that your long running task invokes them when it needs to update the display. The Runnable passed to the method should do a quick update of a GUI component and return.

I haven't used ProgressMonitorDialog, but I believe that by passing the fork parameter of true on the run method, it handles the threading for you. Have you tried removing the syncExec call and having the loop with the sleep in it right after the monitor.beginTask call?
Re: Progressbar is frozen with syncExec [message #1378356 is a reply to message #1377350] Sat, 24 May 2014 07:24 Go to previous message
mark tal is currently offline mark talFriend
Messages: 11
Registered: July 2012
Junior Member
I am already passing fork parameter as true in the code above...

When I remove syncExec call it works, but I won't be able to access gui controls.
Previous Topic:[SOLVED]Accessing GUI from another thread with progressbar
Next Topic:UTF-8 Char are not displaying in TableViewer SWT
Goto Forum:
  


Current Time: Thu Apr 18 05:02:37 GMT 2024

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

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

Back to the top