// Execute an action on the main thread public static void ExecuteOnMainThread(MainThreadExecuteMessage msg) { if (StopRequested) { return; } // Push the action into the main thread queue for execution lock (MainThreadExecuteLocker) { ExecuteOnMainThreadQueue.Add(msg); } }
// Execute an action on the main thread public static void ExecuteOnMainThreadAndWait(MainThreadExecuteMessage msg) { if (StopRequested) { return; } // Make a wait flag var r = new AutoResetEvent(false); // Inject the flag setter at the end of the action msg += () => { r.Set(); }; // Push the action into the main thread queue for execution lock (MainThreadExecuteLocker) { ExecuteOnMainThreadQueue.Add(msg); } // Wait until the flag has been set r.WaitOne(); }