Esempio n. 1
0
        private void Remove(ScheduledAction scheduledAction)
        {
            var pos = _actions.BinarySearch(scheduledAction);

            _actions.RemoveAt(pos);
            scheduledAction.Release();
            if (pos == 0)
            {
                _resetEvent.Set();
            }
        }
Esempio n. 2
0
        private void QueueInternal(ScheduledAction scheduledAction)
        {
            lock (_actions)
            {
                var pos = _actions.BinarySearch(scheduledAction);
                pos = pos >= 0 ? pos : ~pos;
                _actions.Insert(pos, scheduledAction);

                if (pos == 0)
                {
                    _resetEvent.Set();
                }
            }
        }
Esempio n. 3
0
        public void Start()
        {
            _isStopped = false;
            ThreadPool.QueueUserWorkItem(state => {
                ScheduledAction scheduledAction = null;

                while (!_isStopped)
                {
                    bool any;
                    lock (_actions)
                    {
                        any = _actions.Count > 0;
                        if (any)
                        {
                            scheduledAction = _actions[0];
                        }
                    }

                    TimeSpan timeToWait;
                    if (any)
                    {
                        var runTime = scheduledAction.NextExecutionDate;
                        var dT      = runTime - DateTimeProvider.UtcNow;
                        timeToWait  = dT > TimeSpan.Zero ? dT : TimeSpan.Zero;
                    }
                    else
                    {
                        timeToWait = TimeSpan.FromMilliseconds(-1);
                    }

                    if (_resetEvent.WaitOne(timeToWait, false))
                    {
                        continue;
                    }

                    Debug.Assert(scheduledAction != null, "scheduledAction != null");
                    scheduledAction.Execute();
                    lock (_actions)
                    {
                        Remove(scheduledAction);
                        if (scheduledAction.Repeat)
                        {
                            QueueForever(scheduledAction.Action, scheduledAction.Interval);
                        }
                    }
                }
            });
        }
Esempio n. 4
0
 public void QueueOneTime(Action action, TimeSpan interval)
 {
     QueueInternal(ScheduledAction.Create(action, interval, false));
 }
Esempio n. 5
0
 public void QueueForever(Action action, TimeSpan interval)
 {
     QueueInternal(ScheduledAction.Create(action, interval, true));
 }