Beispiel #1
0
        /// <inheritdoc cref="IActionScheduler.Schedule"/>
        public ScheduledAction?Schedule(DateTimeOffset time, Func <Task> action)
        {
            if ((time - DateTimeOffset.Now).TotalSeconds <= 0)
            {
                return(null);
            }

            var task = new ScheduledAction
            {
                Time   = time,
                Action = action,
                CancellationTokenSource = new CancellationTokenSource()
            };

            task.Delay = CreateExtendedDelay(time.ToUnixTimeMilliseconds() - DateTimeOffset.Now.ToUnixTimeMilliseconds()).ContinueWith(task => action(), task.CancellationTokenSource.Token).ContinueWith(t =>
            {
                _actions.Remove(task);
            }, task.CancellationTokenSource.Token);
            _actions.Add(task);
            return(task);
        }
Beispiel #2
0
 /// <inheritdoc cref="IActionScheduler.Unschedule"/>
 public bool Unschedule(ScheduledAction action)
 {
     action.CancellationTokenSource.Cancel();
     return(_actions.Remove(action));
 }