Exemplo n.º 1
0
 /// <summary>
 /// Raises the DoWork event.
 /// </summary>
 /// <param name="e">A <see cref="QueuedWorkerDoWorkEventArgs"/> that contains event data.</param>
 protected virtual void OnDoWork(QueuedWorkerDoWorkEventArgs e)
 {
     if (DoWork != null)
     {
         DoWork(this, e);
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Used by the worker thread to process items.
        /// </summary>
        private void Run()
        {
            while (!Stopping)
            {
                lock (_lockObject)
                {
                    // Wait until we have pending work items
                    if (IsWorkQueueEmpty())
                    {
                        Monitor.Wait(_lockObject);
                    }
                }

                // Loop until we exhaust the queue
                var queueFull = true;
                while (queueFull && !Stopping)
                {
                    // Get an item from the queue
                    AsyncOperation asyncOp  = null;
                    object         request  = null;
                    var            priority = 0;
                    lock (_lockObject)
                    {
                        // Check queues
                        var work = GetWork();
                        asyncOp  = work.Item1;
                        priority = work.Item2;
                        if (asyncOp != null)
                        {
                            request = asyncOp.UserSuppliedState;
                        }

                        // Check if the item was removed
                        if (request != null && _cancelledItems.ContainsKey(request))
                        {
                            request = null;
                        }
                    }

                    if (request != null)
                    {
                        Exception error = null;
                        // Start the work
                        var arg = new QueuedWorkerDoWorkEventArgs(request, priority);
                        try
                        {
                            // Raise the do work event
                            OnDoWork(arg);
                        }
                        catch (Exception e)
                        {
                            error = e;
                        }

                        // Raise the work complete event
                        var arg2 = new QueuedWorkerCompletedEventArgs(request,
                                                                      arg.Result, priority, error, arg.Cancel);
                        if (!Stopping)
                        {
                            asyncOp.PostOperationCompleted(_workCompletedCallback, arg2);
                        }
                    }
                    else if (asyncOp != null)
                    {
                        asyncOp.OperationCompleted();
                    }

                    // Check if the cache is exhausted
                    lock (_lockObject)
                    {
                        queueFull = !IsWorkQueueEmpty();
                    }
                }
            }
        }