예제 #1
0
        /// <summary>
        /// Raises the InvokeCompleted event.
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnInvokeCompleted(InvokeCompletedEventArgs e)
        {
            EventHandler <InvokeCompletedEventArgs> handler = InvokeCompleted;

            if (handler != null)
            {
                context.Post(delegate(object state)
                {
                    handler(this, e);
                }, null);
            }
        }
예제 #2
0
        // Processes and invokes delegates.
        private void DelegateProcedure()
        {
            lock (lockObject)
            {
                // Signal the constructor that the thread is now running.
                Monitor.Pulse(lockObject);
            }

            // Set this DelegateQueue as the SynchronizationContext for this thread.
            SynchronizationContext.SetSynchronizationContext(this);

            // Placeholder for DelegateQueueAsyncResult objects.
            DelegateQueueAsyncResult result = null;

            // While the DelegateQueue has not been disposed.
            while (true)
            {
                // Critical section.
                lock (lockObject)
                {
                    // If the DelegateQueue has been disposed, break out of loop; we're done.
                    if (disposed)
                    {
                        break;
                    }

                    // If there are delegates waiting to be invoked.
                    if (delegateDeque.Count > 0)
                    {
                        result = delegateDeque.Dequeue();
                    }
                    // Else there are no delegates waiting to be invoked.
                    else
                    {
                        // Wait for next delegate.
                        Monitor.Wait(lockObject);

                        // If the DelegateQueue has been disposed, break out of loop; we're done.
                        if (disposed)
                        {
                            break;
                        }
                        result = delegateDeque.Dequeue();
                    }
                }

                // Invoke the delegate.
                result.Invoke();

                if (result.NotificationType == NotificationType.BeginInvokeCompleted)
                {
                    InvokeCompletedEventArgs e = new InvokeCompletedEventArgs(
                        result.Method,
                        result.GetArgs(),
                        result.ReturnValue,
                        result.Error);

                    OnInvokeCompleted(e);
                }
                else if (result.NotificationType == NotificationType.PostCompleted)
                {
                    object[] args = result.GetArgs();

                    PostCompletedEventArgs e = new PostCompletedEventArgs(
                        (SendOrPostCallback)result.Method,
                        args[0],
                        result.Error);

                    OnPostCompleted(e);
                }
            }
        }