Example #1
0
        /// <summary>
        /// Schedules a periodic piece of work, using a System.Threading.Timer 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="ArgumentOutOfRangeException"><paramref name="period"/> is less than TimeSpan.Zero.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception>
        public IDisposable SchedulePeriodic <TState>(TState state, TimeSpan period, Func <TState, TState> action)
        {
            if (period < TimeSpan.Zero)
            {
                throw new ArgumentOutOfRangeException(nameof(period));
            }
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

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

            var cancel = s_cal.StartPeriodicTimer(() =>
            {
                gate.Wait(() =>
                {
                    state1 = action(state1);
                });
            }, period);

            return(Disposable.Create(() =>
            {
                cancel.Dispose();
                gate.Dispose();
                action = Stubs <TState> .I;
            }));
        }
Example #2
0
            public void Dispose()
            {
                var timer = _timer;

                if (timer != null)
                {
                    _action = Stubs <TState> .I;
                    _timer  = null;

                    timer.Dispose();
                    _gate.Dispose();
                }
            }
        /// <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;
            }));
        }
Example #4
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), Strings_PlatformServices.WINRT_NO_SUB1MS_TIMERS);
            }
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

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

            var res = global::Windows.System.Threading.ThreadPoolTimer.CreatePeriodicTimer(
                tpt =>
            {
                gate.Wait(() =>
                {
                    state1 = action(state1);
                });
            },
                period
                );

            return(Disposable.Create(() =>
            {
                res.Cancel();
                gate.Dispose();
                action = Stubs <TState> .I;
            }));
        }
Example #5
0
        public void Dispose()
        {
            var l = new AsyncLock();

            var l1 = false;
            var l2 = false;
            var l3 = false;
            var l4 = false;

            l.Wait(() =>
            {
                l.Wait(() =>
                {
                    l.Wait(() =>
                    {
                        l3 = true;
                    });

                    l2 = true;

                    l.Dispose();

                    l.Wait(() =>
                    {
                        l4 = true;
                    });
                });

                l1 = true;
            });

            Assert.IsTrue(l1);
            Assert.IsTrue(l2);
            Assert.IsFalse(l3);
            Assert.IsFalse(l4);
        }
Example #6
0
 public void Dispose()
 {
     _cts.Cancel();
     _gate.Dispose();
 }
 public void Dispose()
 {
     Disposable.TryDispose(ref _task);
     _gate.Dispose();
 }
 public void Dispose()
 {
     _timer.Cancel();
     _gate.Dispose();
     _action = Stubs <TState> .I;
 }
 public void Dispose()
 {
     _cancel.Dispose();
     _gate.Dispose();
     _action = Stubs <TState> .I;
 }