* @throws InterruptedException
*/
public static void runBlockingOperation( final IRunnableWithProgress runnable,
final IProgressMonitor monitor2 ) throws InvocationTargetException, InterruptedException {
final IProgressMonitor monitor=monitor2==null?new NullProgressMonitor():monitor2;
final InterruptedException[] interruptedException = new InterruptedException[1];
final InvocationTargetException[] invocationTargetException = new InvocationTargetException[1];
Display d = Display.getCurrent();
if (d == null)
d = Display.getDefault();
final Display display = d;
final AtomicBoolean done = new AtomicBoolean();
final Object mutex=new Object();
done.set(false);
Future<Object> future = executor.submit(new Callable<Object>(){
@SuppressWarnings("unused")
Exception e = new Exception("For debugging"); //$NON-NLS-1$
public Object call() throws Exception {
try {
runnable.run(new OffThreadProgressMonitor(monitor != null
? monitor
: ProgressManager.instance().get(), display));
} catch (InvocationTargetException ite) {
invocationTargetException[0] = ite;
} catch (InterruptedException ie) {
interruptedException[0] = ie;
} finally {
done.set(true);
synchronized (mutex) {
mutex.notify();
}
}
return null;
}
});
while( !monitor.isCanceled() && !done.get() && !Thread.interrupted() ) {
Thread.yield();
if (Display.getCurrent() == null) {
wait(mutex, 200);
} else {
try {
if (!d.readAndDispatch()) {
wait(mutex, 200);
}
} catch (Exception e) {
UiPlugin.log("Error occurred while waiting for an operation to complete", e); //$NON-NLS-1$
}
}
}
if (monitor.isCanceled()) {
future.cancel(true);
}
if (interruptedException[0] != null)
throw interruptedException[0];