示例#1
0
        private void ExecuteAction(QueueTimerEventArgs e)
        {
            var dw = DoWork;

            if (dw != null)
            {
                try
                {
                    // New jobs can be added to the queue during execution.
                    dw(this, e);
                }
                catch (Exception ex)
                {
                    lastException     = ex.ToString();
                    lastExceptionDate = DateTime.Now;
                    exceptionCount++;
                }
            }
        }
示例#2
0
        protected void ThreadAction(object state)
        {
            if (string.IsNullOrEmpty(Thread.CurrentThread.Name))
            {
                Thread.CurrentThread.Name = "QueueTimerThread";
            }
            ThreadCount++;
            T   item            = null;
            var firstRun        = true;
            var cancelExecution = false;

            while (true)
            {
                lock (queueLock)
                {
                    // If no arguments left then leave the loop (except if this is firs run.
                    if (!firstRun && (Queue.Count == 0 || IsDisposing || cancelExecution))
                    {
                        SleepTimerStart();
                        // Start sleep timer.
                        _LastActionDoneTime.Reset();
                        _LastActionDoneTime.Start();
                        // Mark thread as not running;
                        IsRunning = false;
                        return;
                    }
                    if (Queue.Count > 0)
                    {
                        item = Queue[0];
                        processingFirstItem = true;
                        ActionCount++;
                    }
                    else
                    {
                        ActionNoneCount++;
                    }
                    firstRun = false;
                }
                // Note: Queue could be cleared before next queueLock is taken.
                var e = new QueueTimerEventArgs();
                e.Item = item;
                // Do synchronous action.
                ExecuteAction(e);
                cancelExecution = e.Cancel;
                // Remove item after job complete.
                lock (queueLock)
                {
                    // If thread item was taken then...
                    if (item != null && !e.Keep)
                    {
                        // Fire event before removing.
                        var br = BeforeRemove;
                        if (br != null)
                        {
                            br(this, e);
                        }
                        // Remove item from the queue (mostly ALWAYS it will be the first item in the queue _Queue[0]).
                        if (Queue.Contains(item))
                        {
                            Queue.Remove(item);
                        }
                        processingFirstItem = false;
                    }
                }
            }
        }