Esempio n. 1
0
        /// <summary>
        /// Schedules an action to be executed.
        /// </summary>
        /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
        /// <param name="state">State passed to the action to be executed.</param>
        /// <param name="action">Action to be executed.</param>
        /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
        /// <exception cref="ArgumentNullException"><paramref name="action"/> is <c>null</c>.</exception>
        public override IDisposable Schedule <TState>(TState state, Func <IScheduler, TState, IDisposable> action)
        {
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            var workItem = new UserWorkItem <TState>(this, state, action);

            ThreadPool.QueueUserWorkItem(
                closureWorkItem => ((UserWorkItem <TState>)closureWorkItem).Run(),
                workItem
                );

            return(workItem);
        }
Esempio n. 2
0
        /// <summary>
        /// Schedules an action to be executed.
        /// </summary>
        /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
        /// <param name="state">State passed to the action to be executed.</param>
        /// <param name="action">Action to be executed.</param>
        /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
        /// <exception cref="ArgumentNullException"><paramref name="action"/> is <c>null</c>.</exception>
        public override IDisposable Schedule <TState>(TState state, Func <IScheduler, TState, IDisposable> action)
        {
            if (null == action)
            {
                throw new ArgumentNullException(nameof(action));
            }

            var workItem = new UserWorkItem <TState>(this, state, action);

            workItem.CancelQueueDisposable = concurrency.QueueUserWorkItem(
                closureWorkItem => ((UserWorkItem <TState>)closureWorkItem).Run(),
                workItem
                );

            return(workItem);
        }
Esempio n. 3
0
        /// <summary>
        /// Schedules an action to be executed after dueTime, using a System.Threading.Timer object.
        /// </summary>
        /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
        /// <param name="state">State passed to the action to be executed.</param>
        /// <param name="action">Action to be executed.</param>
        /// <param name="dueTime">Relative time after which to execute the action.</param>
        /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
        /// <exception cref="ArgumentNullException"><paramref name="action"/> is <c>null</c>.</exception>
        public override IDisposable Schedule <TState>(TState state, TimeSpan dueTime, Func <IScheduler, TState, IDisposable> action)
        {
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            var dt = Scheduler.Normalize(dueTime);

            if (dt.Ticks == 0)
            {
                return(Schedule(state, action));
            }

            var workItem = new UserWorkItem <TState>(this, state, action);

            workItem.CancelQueueDisposable = new Timer(
                closureWorkItem => ((UserWorkItem <TState>)closureWorkItem).Run(),
                workItem,
                dt,
                Timeout.InfiniteTimeSpan);

            return(workItem);
        }
Esempio n. 4
0
        /// <summary>
        /// Schedules an action to be executed after dueTime, using a System.Threading.Timer object.
        /// </summary>
        /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
        /// <param name="state">State passed to the action to be executed.</param>
        /// <param name="action">Action to be executed.</param>
        /// <param name="dueTime">Relative time after which to execute the action.</param>
        /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
        /// <exception cref="ArgumentNullException"><paramref name="action"/> is <c>null</c>.</exception>
        public override IDisposable Schedule <TState>(TState state, TimeSpan dueTime, Func <IScheduler, TState, IDisposable> action)
        {
            if (null == action)
            {
                throw new ArgumentNullException(nameof(action));
            }

            var dt = Scheduler.Normalize(dueTime);

            if (0 == dt.Ticks)
            {
                return(Schedule(state, action));
            }

            var workItem = new UserWorkItem <TState>(this, state, action);

            workItem.CancelQueueDisposable = concurrency.StartTimer(
                closureWorkItem => ((UserWorkItem <TState>)closureWorkItem).Run(),
                workItem,
                dt
                );

            return(workItem);
        }