public override void Post(SendOrPostCallback d, object state) { // queue the item and don't wait for its execution. This is risky because // an unhandled exception will terminate the STA thread. Use with caution. SendOrPostCallbackItem item = new SendOrPostCallbackItem(d, state, ExecutionType.Post); _queue.Enqueue(item); }
private void Run() { this.ManagedThreadId = Thread.CurrentThread.ManagedThreadId; while (true) { bool stop = _stopEvent.WaitOne(0); if (stop) { break; } SendOrPostCallbackItem workItem = _consumerQueue.Dequeue(); if (workItem != null) { workItem.Execute(); } } }
public override void Send(SendOrPostCallback d, object state) { // to avoid deadlock! if (Thread.CurrentThread.ManagedThreadId == _staThread.ManagedThreadId) { d(state); return; } // create and queue an item for execution SendOrPostCallbackItem item = new SendOrPostCallbackItem(d, state, ExecutionType.Send); _queue.Enqueue(item); // wait for the item execution to end item.ExecutionCompleteWaitHandle.WaitOne(); // if there was an exception, throw it on the caller thread, not the sta thread. if (item.ExecutedWithException) { throw item.Exception; } }