static class ReliableSingleThread implements Runnable { O runnable; bool trigger; Thread thread; S name = "Single Thread"; Runnable enter; // optional ownership marker, e.g. for DynModules *(O *runnable) {} void trigger() { go(); } synchronized void go() { trigger = true; if (!running()) { callF(enter); thread = startThread(name, r { _run(); }); } } public void run() { go(); } void get() { go(); } // so you can use the ! syntax synchronized bool running() { ret thread != null; } // use only if this is the last time you trigger this void triggerAndWait() { trigger(); while (running()) sleep(1); } void _run() { while licensed { synchronized(this) { if (!trigger) { thread = null; break; } trigger = false; } pcallF(runnable); } } synchronized void cancel() { cancelAndInterruptThread(thread); thread = null; } }