Using Jobs API and asyncExec
Using the Jobs API (Job and IProgressMonitor) you can implement code that will run in a thread and update the standard Eclipse progress monitoring service as you go along in your thread.
To create a job, extend from org.eclipse.core.runtime.Job and implement the run method:
protected IStatus run(IProgressMonitor monitor) { int steps = 100000; monitor.beginTask("My Task", steps); for ( int i=0; i<steps ; i++ ) { monitor.subTask("Step "+i); monitor.worked(i); if ( monitor.isCanceled() ) break; } inputForTableViewer = Arrays.asList(new String[] {"One", "Two", "Three"}); return Status.OK_STATUS; }
To activate the job in MyClass (say it's a page with a tableViewer which we use to set the input), you do the following:
MySampleJob myJob = new MySampleJob(); myJob .addJobChangeListener(new JobChangeAdapter() { public void done(IJobChangeEvent event) { Display.getDefault().asyncExec(new Runnable() { public void run() { MyClass.this.tableViewer. setInput(myJob .getInputForTableViewer()); } }); } }); myJob.schedule();
Have fun!