Esempio n. 1
0
        /// <inheritdoc />
        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 d = new SingleAssignmentDisposable();

            WasmRuntime.ScheduleTimeout(
                (int)dt.TotalMilliseconds,
                () =>
            {
                if (!d.IsDisposed)
                {
                    d.Disposable = action(this, state);
                }
            });

            return(d);
        }
Esempio n. 2
0
        /// <summary>
        /// Schedules a periodic piece of work, using a Windows.System.Threading.ThreadPoolTimer object.
        /// </summary>
        /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
        /// <param name="state">Initial state passed to the action upon the first iteration.</param>
        /// <param name="period">Period for running the work periodically.</param>
        /// <param name="action">Action to be executed, potentially updating the state.</param>
        /// <returns>The disposable object used to cancel the scheduled recurring action (best effort).</returns>
        /// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="period"/> is less than one millisecond.</exception>
        public IDisposable SchedulePeriodic <TState>(TState state, TimeSpan period, Func <TState, TState> action)
        {
            // The WinRT thread pool is based on the Win32 thread pool and cannot handle
            // sub-1ms resolution. When passing a lower period, we get single-shot
            // timer behavior instead. See MSDN documentation for CreatePeriodicTimer
            // for more information.
            if (period < TimeSpan.FromMilliseconds(1))
            {
                throw new ArgumentOutOfRangeException(nameof(period), "The WinRT thread pool doesn't support creating periodic timers with a period below 1 millisecond.");
            }

            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            var state1 = state;
            var gate   = new AsyncLock();

            WasmRuntime.ScheduleTimeout(
                (int)period.TotalMilliseconds,
                () =>
            {
                Action run = null;

                run = () =>
                {
                    gate.Wait(() =>
                    {
                        state1 = action(state1);

                        WasmRuntime.ScheduleTimeout(
                            (int)period.TotalMilliseconds,
                            run);
                    });
                };
            });

            return(Disposable.Create(() =>
            {
                gate.Dispose();
                action = Stubs <TState> .I;
            }));
        }
Esempio n. 3
0
        /// <inheritdoc />
        public override IDisposable Schedule <TState>(TState state, Func <IScheduler, TState, IDisposable> action)
        {
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            var d = new SingleAssignmentDisposable();

            WasmRuntime.ScheduleTimeout(0, () =>
            {
                if (!d.IsDisposed)
                {
                    d.Disposable = action(this, state);
                }
            });

            return(d);
        }