Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Minimal SWT Console App(How to write a correct threaded SWT application)
Minimal SWT Console App [message #1235324] Fri, 24 January 2014 04:01 Go to next message
Chris Gage is currently offline Chris GageFriend
Messages: 74
Registered: July 2009
Member
I'm trying to convert a batch command line program so it has a minimal SWT GUI, and this is my first SWT program. I have however been writing Java since 1996 and Eclipse since 2001 (WSAD4) and was team lead for IBM's first ever Java-based product. Now blissfully retired. This present application is a labor of love, and I possibly might open-source it if I can get some issues like this sorted out.

So I looked everywhere for an example of a "console" application in SWT, but could not find one. Eventually I learned about asyncExec and Runnables and a few other necessities, and came up with the following, but it clearly isn't correct. It does the job, with the Worker class representing the main body of my code, but the GUI window is locked while the application is writing to it. Only after the application is finished running can I move or size the window. At the end of the run the window does contain the correct data, but it might run for 5 or 10 minutes, and during that time I might want to move its window.

I'm sure I have missed something fundamental. I know I can move Eclipse's window while it is doing things in the background, so I need to replicate that ability (without thousands of loc).

Comments and suggestions appreciated.

public class Console2 {
	Display display;
	Shell shell;
	Text text;

	private void process() {
		display = new Display();
		shell = new Shell(display);
		shell.setLayout(new FillLayout());
		shell.setLocation(new Point(80, 80));
		text = new Text(shell, SWT.MULTI | SWT.V_SCROLL | SWT.READ_ONLY);
		shell.open();
		Display.getDefault().asyncExec(new Runnable() {
			public void run() {
				new Worker(text).work();
			}
		});
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}

	class Worker {
		Text text;
		public Worker(Text t) {
			text = t;
		};
		void work() {
			for (int i = 0; i < 10000; i++) {
				if (!text.isDisposed()) {
					text.append("hello " + i + "\n");
				}
			}
		}
	}
	public static void main(String[] args) {
		new Console2().process();
	}
}

[Updated on: Fri, 24 January 2014 04:03]

Report message to a moderator

Re: Minimal SWT Console App [message #1235501 is a reply to message #1235324] Fri, 24 January 2014 14:18 Go to previous message
David Wegener is currently offline David WegenerFriend
Messages: 1445
Registered: July 2009
Senior Member
The asyncExec call invokes the run method on the Display thread. That means that the Display can't do anything until the run method returns. In your case the method doesn't return until the work method completes. Your work method would be considered a long running method. This means that that method should not be called from an asyncExec Runnable. The only piece of code that should be in the asyncExec Runnable is the isDisposed check and the text.append call. The rest of the work method needs to be executed in a separate Thread/Job.
Previous Topic:Spinner no setText
Next Topic:Compilation of HelloWorldSWT fails with "no swt-gtk-4333 in java.library.path"
Goto Forum:
  


Current Time: Thu Apr 25 22:27:05 GMT 2024

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

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

Back to the top