Пример #1
0
        public object Invoke(Delegate method, params object[] args)
        {
            if (disposed)
            {
                throw new ObjectDisposedException("Queue");
            }
            else if (method == null)
            {
                throw new ArgumentNullException();
            }

            object returnValue = null;

            if (InvokeRequired)
            {
                var result = new QueueAsyncResult(this, method, args);

                lock (lockObject)
                {
                    queue.Enqueue(result);
                    Monitor.Pulse(lockObject);
                }

                returnValue = EndInvoke(result);
            }
            else
            {
                returnValue = method.DynamicInvoke(args);
            }

            return(returnValue);
        }
Пример #2
0
        private void Procedure()
        {
            lock (lockObject)
            {
                Monitor.Pulse(lockObject);
            }

            SetSynchronizationContext(this);
            QueueAsyncResult result = null;

            while (true)
            {
                lock (lockObject)
                {
                    if (disposed)
                    {
                        break;
                    }

                    if (queue.Count > 0)
                    {
                        result = queue.Dequeue();
                    }
                    else
                    {
                        Monitor.Wait(lockObject);

                        if (disposed)
                        {
                            break;
                        }

                        result = queue.Dequeue();
                    }
                }

                result.Invoke();
            }
        }