org.
eclipse.swt.widgets.Display.readAndDispatch()将处理事件队列中的事件,如果没有其他事件要处理,则返回false.但您可能不希望在处理事件时使用它.
asyncExec(*)是一个FIFO队列(虽然OS图形事件取代了asyncExecs),因此您可以执行大部分长时间运行的op处理,然后在队列中放置最终的asyncExec:
final boolean[] done = new boolean[1];
Runnable r = new Runnable() {
public void run() {
done[0] = true;
}
};
// now wait for the event somehow. The brute force method:
while (!done[0]) {
Thread.sleep(200);
}
理论上,从你长时间运行的op中产生的所有其他asyncExecs将在你到达最后一个时完成.
编辑:潜在的其他选择
创建自己的org.eclipse.core.runtime.jobs.Job,然后在最后加入()它:
public static class RefCountJob extends Job {
public RefCountJob() {
super("REF_COUNT");
}
int count = 0;
public void increment() {
count++;
}
public void decrement() {
count--;
}
@Override
protected IStatus run(IProgressMonitor monitor) {
monitor.beginTask("WAITING", IProgressMonitor.UNKNOWN);
while (count > 0) {
Thread.sleep(200);
monitor.worked(1);
}
monitor.done();
return Status.OK_STATUS;
}
}
要使用它,每次你要发射事件时递增()它,并在它们完成时让它们减少它(你必须确保它们减少它而不管抛出什么异常:-)
RefCountJob ref = new RefCountJob();
// ... do stuff, everybody increments and decrements ref
ref.increment();
// ... do more stuff
ref.increment();
// at the end of your long-running job
ref.schedule();
ref.join();