예제 #1
0
 private void Remove(ScheduledAction scheduledAction)
 {
     lock (_actions)
     {
         var pos = _actions.BinarySearch(scheduledAction);
         _actions.RemoveAt(pos);
         scheduledAction.Release();
         if (pos == 0)
         {
             _resetEvent.Set();
         }
     }
 }
예제 #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();
                }
            }
        }
예제 #3
0
        public void Start()
        {
            Task.Factory.StartNew(() =>
            {
                ScheduledAction scheduledAction = null;

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

                    TimeSpan timeToWait;
                    if (any)
                    {
                        DateTime runTime = scheduledAction.NextExecutionDate;
                        var dT           = runTime - DateTime.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);
                        }
                    }
                }
            }, _cancellationTokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
        }
예제 #4
0
 public void QueueOneTime(Action action, TimeSpan interval)
 {
     QueueInternal(ScheduledAction.Create(action, interval, false));
 }
예제 #5
0
 public void QueueForever(Action action, TimeSpan interval)
 {
     QueueInternal(ScheduledAction.Create(action, interval, true));
 }