public void WriteVerbose(string text) { // if we are on the original thread, just call straight thru. if (this.originalThread == System.Threading.Thread.CurrentThread) { originalCommandRuntime.WriteVerbose(text); return; } // otherwise, queue up the request and wait for the main thread to do the right thing. try { // wait for our turn to talk to the main thread WaitOurTurn(); // set the function to run runOnMainThread = () => originalCommandRuntime.WriteVerbose(text); // tell the main thread to go ahead readyToRun.Set(); // wait for the result (or cancellation!) WaitForCompletion(); // return return; } catch (System.OperationCanceledException exception) { // maybe don't even worry? throw exception; } }