Exemple #1
0
        private IScheduledTask ScheduleInternal(TimeSpan initialDelay, TimeSpan period,
                                                Func <CancellationToken, Task> action, Func <IFailureContext, CancellationToken, Task> onException,
                                                TimeSpan maxBackoffPeriod)
        {
            SchedulerAssertions.Assert(initialDelay, period, action, onException, maxBackoffPeriod);

            var cancellationTokenSource = new CancellationTokenSource();
            var cancellationToken       = cancellationTokenSource.Token;

            var task = Task.Run(async() =>
            {
                using (cancellationTokenSource)
                {
                    var failureContext = new FailureContext(period, maxBackoffPeriod, () => Now);

                    if (initialDelay > TimeSpan.Zero)
                    {
                        await Task.Delay(initialDelay, cancellationToken);
                        await CallAction(action, onException, cancellationToken, failureContext);
                    }

                    while (!cancellationToken.IsCancellationRequested)
                    {
                        await Task.Delay(failureContext.CurrentPeriod, cancellationToken);
                        await CallAction(action, onException, cancellationToken, failureContext);
                    }
                }
            }, cancellationToken);

            return(new ScheduledTask(task, cancellationTokenSource));
        }
Exemple #2
0
        private IScheduledTask ScheduleInternal(TimeSpan initialDelay, TimeSpan period,
                                                Func <CancellationToken, Task> action, Func <IFailureContext, CancellationToken, Task> onException,
                                                TimeSpan maxBackoffPeriod)
        {
            SchedulerAssertions.Assert(initialDelay, period, action, onException, maxBackoffPeriod);

            var t = new TestScheduledTask(Now.Add(initialDelay), period, action, onException,
                                          maxBackoffPeriod, () => Now);

            _timers.TryAdd(t, null);

            return(t);
        }