public override Task ScheduleAsync(Action action, TimeSpan delay, CancellationToken cancellationToken)
        {
            Contract.Requires(action != null);

            if (cancellationToken.IsCancellationRequested)
            {
                return(TaskEx.Cancelled);
            }

            if (!cancellationToken.CanBeCanceled)
            {
                return(this.Schedule(action, delay).Completion);
            }

            return(this.Schedule(new ActionScheduledAsyncTask(this, action, PreciseTimeSpan.Deadline(delay), cancellationToken)).Completion);
        }
        bool RunAllTasks(PreciseTimeSpan timeout)
        {
            this.FetchFromScheduledTaskQueue();
            IRunnable task = this.PollTask();

            if (task == null)
            {
                return(false);
            }

            PreciseTimeSpan deadline = PreciseTimeSpan.Deadline(timeout);
            long            runTasks = 0;
            PreciseTimeSpan executionTime;

            while (true)
            {
                SafeExecute(task);

                runTasks++;

                // Check timeout every 64 tasks because nanoTime() is relatively expensive.
                // XXX: Hard-coded value - will make it configurable if it is really a problem.
                if ((runTasks & 0x3F) == 0)
                {
                    executionTime = PreciseTimeSpan.FromStart;
                    if (executionTime >= deadline)
                    {
                        break;
                    }
                }

                task = this.PollTask();
                if (task == null)
                {
                    executionTime = PreciseTimeSpan.FromStart;
                    break;
                }
            }

            this.lastExecutionTime = executionTime;
            return(true);
        }
        public override IScheduledTask Schedule(IRunnable action, TimeSpan delay)
        {
            Contract.Requires(action != null);

            return(this.Schedule(new RunnableScheduledTask(this, action, PreciseTimeSpan.Deadline(delay))));
        }
        public override Task ScheduleAsync(Action <object, object> action, object context, object state, TimeSpan delay, CancellationToken cancellationToken)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                return(TaskEx.Cancelled);
            }

            if (!cancellationToken.CanBeCanceled)
            {
                return(this.Schedule(action, context, state, delay).Completion);
            }

            return(this.Schedule(new StateActionWithContextScheduledAsyncTask(this, action, context, state, PreciseTimeSpan.Deadline(delay), cancellationToken)).Completion);
        }
        public override IScheduledTask Schedule(Action <object, object> action, object context, object state, TimeSpan delay)
        {
            Contract.Requires(action != null);

            return(this.Schedule(new StateActionWithContextScheduledTask(this, action, context, state, PreciseTimeSpan.Deadline(delay))));
        }