If the command takes a long time to execute, the user interface can be blocked. This happens because there is only one user interface thread, and because the command is launched from the UI, it will run in the UI thread. Instead, long running operations should run in a background thread, and then once finished, be able to display the results instead. Clearly creating a new Thread
(like the clock updates initially) or other techniques like a Timer
would work. However, the Eclipse system has a mechanism to provide a Job
to do the work instead, or a UIJob
to run in the context of the UI thread.
Open the
HelloHandler
and go to theexecute()
method. Replace its contents with the following code snippet:public Object execute(ExecutionEvent event) { Job job = new Job("About to say hello") { protected IStatus run(IProgressMonitor monitor) { try { Thread.sleep(5000); } catch (InterruptedException e) { } MessageDialog...