Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Problem with asyncExec() - freeze app
Problem with asyncExec() - freeze app [message #830862] Wed, 28 March 2012 06:27 Go to next message
marioosh Missing name is currently offline marioosh Missing nameFriend
Messages: 2
Registered: March 2012
Junior Member
I have some code inside asyncExec() call, but it freeze gui. Why ? Full code below.


import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.StatusLineManager;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridLayout;


public class TestApp extends ApplicationWindow {

	/**
	 * Create the application window.
	 */
	public TestApp() {
		super(null);
		createActions();
		addToolBar(SWT.FLAT | SWT.WRAP);
		addMenuBar();
		addStatusLine();
	}

	/**
	 * Create contents of the application window.
	 * @param parent
	 */
	@Override
	protected Control createContents(Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);
		container.setLayout(new GridLayout(1, false));
		{
			Button btnNewButton_1 = new Button(container, SWT.NONE);
			btnNewButton_1.setText("Freezed");
		}
		
		Button btnNewButton = new Button(container, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Runnable task = new Runnable() {
					@Override
					public void run() {
						try {
							Thread.sleep(3000);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
						getStatusLineManager().setMessage("Some message");
					}
				};
				Display.getDefault().asyncExec(task);				
			}
		});
		btnNewButton.setText("Some Task");

		return container;
	}

	/**
	 * Create the actions.
	 */
	private void createActions() {
		// Create the actions
	}

	/**
	 * Create the menu manager.
	 * @return the menu manager
	 */
	@Override
	protected MenuManager createMenuManager() {
		MenuManager menuManager = new MenuManager("menu");
		return menuManager;
	}

	/**
	 * Create the toolbar manager.
	 * @return the toolbar manager
	 */
	@Override
	protected ToolBarManager createToolBarManager(int style) {
		ToolBarManager toolBarManager = new ToolBarManager(style);
		return toolBarManager;
	}

	/**
	 * Create the status line manager.
	 * @return the status line manager
	 */
	@Override
	protected StatusLineManager createStatusLineManager() {
		StatusLineManager statusLineManager = new StatusLineManager();
		return statusLineManager;
	}

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			TestApp window = new TestApp();
			window.setBlockOnOpen(true);
			window.open();
			Display.getCurrent().dispose();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Configure the shell.
	 * @param newShell
	 */
	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("New Application");
	}

	/**
	 * Return the initial size of the window.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(450, 300);
	}
}
Re: Problem with asyncExec() - freeze app [message #831208 is a reply to message #830862] Wed, 28 March 2012 16:00 Go to previous messageGo to next message
David Wegener is currently offline David WegenerFriend
Messages: 1351
Registered: July 2009
Senior Member
It looks like you don't completely understand the behavior of asyncExec. You use this method to run code on the GUI thread. While your Runnable's run method is processing the GUI thread can't do anything else. Your run method has a 3 second sleep. Since this will be executed by the GUI thread, your GUI will be locked up for the duration of the sleep.

You most likely have the process backwards, you need to kick off a Job/Thread that sleeps for 3 seconds and then invokes asyncExec with a Runnable that only updates the Status Line.
Re: Problem with asyncExec() - freeze app [message #831720 is a reply to message #831208] Thu, 29 March 2012 08:51 Go to previous messageGo to next message
marioosh Missing name is currently offline marioosh Missing nameFriend
Messages: 2
Registered: March 2012
Junior Member
Could you provide some code to do this right ?

Do You mean somethink like that:

final Runnable task = new Runnable() {
	@Override
	public void run() {
		getStatusLineManager().setMessage("Some message");
	}
};

new Thread() {
	public void run() {
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		Display.getDefault().asyncExec(task);
	}
}.start();

[Updated on: Thu, 29 March 2012 09:30]

Report message to a moderator

Re: Problem with asyncExec() - freeze app [message #831924 is a reply to message #831720] Thu, 29 March 2012 14:25 Go to previous messageGo to next message
David Wegener is currently offline David WegenerFriend
Messages: 1351
Registered: July 2009
Senior Member
That seems to catch the general idea. The long running operation (Thread.sleep(3000) in your case) is performed on a thread separate from the display thread. The asyncExec call is then used to update the display on the display thread.

Re: Problem with asyncExec() - freeze app [message #832730 is a reply to message #831924] Fri, 30 March 2012 14:03 Go to previous message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2151
Registered: July 2009
Senior Member
On 3/29/2012 10:25 AM, David Wegener wrote:
> That seems to catch the general idea. The long running operation
> (Thread.sleep(3000) in your case) is performed on a thread separate from
> the display thread. The asyncExec call is then used to update the
> display on the display thread.

Note that if the point of the sleep() is to just delay for some time
before doing something then Display.timerExec() is a much simpler way to
do this, no additional threads required. Example snippet:
http://git.eclipse.org/c/platform/eclipse.platform.swt.git/tree/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet60.java
..

Grant
Previous Topic:SWT error while trying to create JavaServer Faces project
Next Topic:How to restrict user from going to Next Page if all the fields are not populated in Eclipse SWT
Goto Forum:
  


Current Time: Thu Dec 18 18:29:02 GMT 2014

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

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