Esempio n. 1
0
        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)
            {
                try
                {
                    task.Run();
                }
                catch (Exception ex)
                {
                    //Logger.Warn("A task raised an exception.", ex);
                }

                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);
        }
Esempio n. 2
0
        public virtual Task ScheduleAsync(Action action, TimeSpan delay, CancellationToken cancellationToken)
        {
            Require.NotNull(action);

            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)
            {
                try
                {
                    task.Run();
                }
                catch (Exception ex)
                {
                    //Logger.Warn("A task raised an exception.", ex);
                }

                runTasks++;

                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);
        }
Esempio n. 4
0
        public virtual 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 StateWithContextScheduledAsyncTask(this, action, context, state, PreciseTimeSpan.Deadline(delay), cancellationToken)).Completion);
        }
Esempio n. 5
0
        public virtual IScheduledTask Schedule(Action <object, object> action, object context, object state, TimeSpan delay)
        {
            Require.NotNull(action);

            return(this.Schedule(new StateActionWithContextScheduledTask(this, action, context, state, PreciseTimeSpan.Deadline(delay))));
        }
Esempio n. 6
0
        public virtual IScheduledTask Schedule(Action action, TimeSpan delay)
        {
            Require.NotNull(action);

            return(this.Schedule(new ActionScheduledTask(this, action, PreciseTimeSpan.Deadline(delay))));
        }