private void execOffThreadWithOnThreadEventDispatch(Runnable testAction) throws Exception { _testMouseListener.setModifierCheckEnabled(false); _robot.setAutoDelay(MS_ROBOT_AUTO_DELAY); { // Make sure all the buttons and modifier keys are released. clearKeyboadAndMouse(); } _testMouseListener.setModifierCheckEnabled(true); Throwable throwable = null; // final Object sync = new Object(); final RunnableTask rt = new RunnableTask(testAction, null, true, System.err); try { // synchronized(sync) { new Thread(rt, "Test-Thread").start(); int i = 0; while (!rt.isExecuted() && null == throwable) { System.err.println("WAIT-till-done: eventDispatch() #" + i++); eventDispatch(); } if (null == throwable) { throwable = rt.getThrowable(); } if (null != throwable) { throw new RuntimeException(throwable); } // } } finally { System.err.println("WAIT-till-done: DONE"); _testMouseListener.setModifierCheckEnabled(false); clearKeyboadAndMouse(); } }
/** invokes the given Runnable */ public void invoke(boolean wait, Runnable r) { if (r == null) { return; } if (NativeWindowFactory.isAWTAvailable()) { initAWTReflection(); // handover to AWT MainThread .. try { if (((Boolean) ReflectionUtil.callMethod(null, mAWTIsDispatchThread, null)) .booleanValue()) { r.run(); return; } if (wait) { ReflectionUtil.callMethod(null, mAWTInvokeAndWait, new Object[] {r}); } else { ReflectionUtil.callMethod(null, mAWTInvokeLater, new Object[] {r}); } } catch (Exception e) { throw new NativeWindowException(e); } return; } // if this main thread is not being used or // if this is already the main thread .. just execute. if (!isRunning() || mainThread == Thread.currentThread()) { r.run(); return; } boolean doWait = wait && isRunning() && mainThread != Thread.currentThread(); Object lock = new Object(); RunnableTask rTask = new RunnableTask(r, doWait ? lock : null, true); Throwable throwable = null; synchronized (lock) { invokeLater(rTask); if (doWait) { try { lock.wait(); } catch (InterruptedException ie) { throwable = ie; } } } if (null == throwable) { throwable = rTask.getThrowable(); } if (null != throwable) { throw new RuntimeException(throwable); } }
private void invokeImpl(boolean wait, Runnable task, boolean stop) { Throwable throwable = null; RunnableTask rTask = null; Object rTaskLock = new Object(); synchronized (rTaskLock) { // lock the optional task execution synchronized (edtLock) { // lock the EDT status if (nedt.shouldStop) { // drop task .. if (DEBUG) { System.err.println( Thread.currentThread() + ": Warning: SWT-EDT about (1) to stop, won't enqueue new task: " + nedt); Thread.dumpStack(); } return; } // System.err.println(Thread.currentThread()+" XXX stop: "+stop+", tasks: // "+edt.tasks.size()+", task: "+task); // Thread.dumpStack(); if (stop) { synchronized (nedt.sync) { nedt.shouldStop = true; nedt.sync.notifyAll(); // stop immediate if waiting (poll freq) } if (DEBUG) { System.err.println( Thread.currentThread() + ": SWT-EDT signal STOP (on edt: " + isCurrentThreadEDT() + ") - " + nedt); // Thread.dumpStack(); } } else if (!nedt.isRunning()) { // start if should not stop && not started yet startImpl(); } if (null == task) { wait = false; } else if (isCurrentThreadEDT()) { task.run(); wait = false; // running in same thread (EDT) -> no wait } else if (swtDisplay.isDisposed()) { wait = false; // drop task, SWT disposed } else { rTask = new RunnableTask( task, wait ? rTaskLock : null, true /* always catch and report Exceptions, don't disturb EDT */); swtDisplay.asyncExec(rTask); } } if (wait) { try { rTaskLock.wait(); // free lock, allow execution of rTask } catch (InterruptedException ie) { throwable = ie; } if (null == throwable) { throwable = rTask.getThrowable(); } if (null != throwable) { if (throwable instanceof NativeWindowException) { throw (NativeWindowException) throwable; } throw new RuntimeException(throwable); } } } }
private final boolean invokeImpl(boolean wait, Runnable task, boolean stop) { Throwable throwable = null; RunnableTask rTask = null; final Object rTaskLock = new Object(); synchronized (rTaskLock) { // lock the optional task execution synchronized (edtLock) { // lock the EDT status if (nedt.shouldStop) { // drop task .. if (DEBUG) { System.err.println( Thread.currentThread() + ": Warning: SWT-EDT about (1) to stop, won't enqueue new task: " + nedt + ", isRunning " + nedt.isRunning + ", shouldStop " + nedt.shouldStop); Thread.dumpStack(); } return false; } if (swtDisplay.isDisposed()) { stop = true; } if (isCurrentThreadEDT()) { if (null != task) { task.run(); } wait = false; // running in same thread (EDT) -> no wait if (stop) { nedt.shouldStop = true; } } else { if (!nedt.isRunning && !swtDisplay.isDisposed()) { if (null != task) { if (stop) { System.err.println( Thread.currentThread() + ": Warning: SWT-EDT is about (3) to stop and stopped already, dropping task. NEDT " + nedt); } else { System.err.println( Thread.currentThread() + ": Warning: SWT-EDT is not running, dropping task. NEDT " + nedt); } if (DEBUG) { Thread.dumpStack(); } } return false; } else if (stop) { if (nedt.isRunning) { if (DEBUG) { System.err.println( Thread.currentThread() + ": SWT-EDT signal STOP (on edt: " + isCurrentThreadEDT() + ") - " + nedt + ", isRunning " + nedt.isRunning + ", shouldStop " + nedt.shouldStop); } synchronized (nedt.sync) { nedt.shouldStop = true; nedt.sync.notifyAll(); // stop immediate if waiting (poll freq) } } if (swtDisplay.isDisposed()) { System.err.println( Thread.currentThread() + ": Warning: SWT-EDT is about (3) to stop and stopped already, dropping task. " + nedt); if (DEBUG) { Thread.dumpStack(); } return false; } } if (null != task) { rTask = new RunnableTask( task, wait ? rTaskLock : null, true /* always catch and report Exceptions, don't disturb EDT */, wait ? null : System.err); swtDisplay.asyncExec(rTask); } } } if (wait) { try { rTaskLock.wait(); // free lock, allow execution of rTask } catch (InterruptedException ie) { throwable = ie; } if (null == throwable) { throwable = rTask.getThrowable(); } if (null != throwable) { if (throwable instanceof NativeWindowException) { throw (NativeWindowException) throwable; } throw new RuntimeException(throwable); } } return true; } }