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.
            var item = new SendOrPostCallbackItem(d, state, ExecutionType.Post);

            mQueue.Enqueue(item);
        }
Exemplo n.º 2
0
 public void AddItem(SendOrPostCallbackItem item)
 {
     lock (((ICollection)mQueue).SyncRoot)
     {
         mQueue.Enqueue(item);
         mItemsInQueueEvent.Set();
     }
 }
Exemplo n.º 3
0
        public SendOrPostCallbackItem Dequeue()
        {
            Console.WriteLine("Before " + mQueue.Count);
            mItemsInQueueEvent.WaitOne();
            Console.WriteLine("After");
            SendOrPostCallbackItem item = null;

            lock (((ICollection)mQueue).SyncRoot)
            {
                if (mQueue.Count > 0)
                {
                    item = mQueue.Dequeue();
                }
            }


            return(item);
        }
        public override void Send(SendOrPostCallback action, object state)
        {
            // to avoid deadlock!
            if (Thread.CurrentThread.ManagedThreadId == mStaThread.ManagedThreadId)
            {
                action(state);
                return;
            }

            // create an item for execution
            var item = new SendOrPostCallbackItem(action, state, ExecutionType.Send);

            // queue the item
            mQueue.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;
            }
        }