public override void Send(SendOrPostCallback callback, object state) { if (callback == null) throw new ArgumentNullException("callback"); // Create an item and use a lambda to convert the SendOrPostCallback to a Func<object,object> SynchronousRunLoopItem<object, object> item = new SynchronousRunLoopItem<object, object>(s => { callback(s); return null; }, state, callback.Method); // Queue up the item at the highest priority (since we'll be waiting for it to finish) _runLoop.Enqueue(item, RunLoop.Priority.High); // Wait for the RunLoop to execute it item.ManualResetEvent.WaitOne(); // Rethrow any exceptions on the current thread if (item.Exception != null) throw item.Exception; }
protected override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued) { // Create an item and use a lambda to call TryExecuteTask() and return it's result SynchronousRunLoopItem<object, object> item = new SynchronousRunLoopItem<object, object>(s => TryExecuteTask(task), null, null); // Queue up the item at the highest priority (since we'll be waiting for it to finish) _runLoop.Enqueue(item, RunLoop.Priority.High); // Wait for the RunLoop to execute it item.ManualResetEvent.WaitOne(); // Rethrow any exceptions on the current thread // (though we shouldn't have any since TryExecuteTask should have handled them) if (item.Exception != null) throw item.Exception; // Return the result return (bool) (item.Result ?? false); }