<PackageReference Include="System.Reactive" Version="4.1.1" />

ThreadPoolScheduler

Represents an object that schedules units of work on the Windows Runtime thread pool.
using System.ComponentModel; using Windows.Foundation; using Windows.System.Threading; namespace System.Reactive.Concurrency { [CLSCompliant(false)] public sealed class ThreadPoolScheduler : LocalScheduler, ISchedulerPeriodic { private sealed class PeriodicallyScheduledWorkItem<TState> : IDisposable { private TState _state; private Func<TState, TState> _action; private readonly ThreadPoolTimer _timer; private readonly AsyncLock _gate = new AsyncLock(); public unsafe PeriodicallyScheduledWorkItem(TState state, TimeSpan period, Func<TState, TState> action) { _state = state; _action = action; _timer = ThreadPoolTimer.CreatePeriodicTimer(new TimerElapsedHandler((object)this, (IntPtr)(void*)), period); } private void Tick(ThreadPoolTimer timer) { _gate.Wait<PeriodicallyScheduledWorkItem<TState>>(this, (Action<PeriodicallyScheduledWorkItem<TState>>)delegate(PeriodicallyScheduledWorkItem<TState> this) { this._state = this._action(this._state); }); } public void Dispose() { _timer.Cancel(); _gate.Dispose(); _action = Stubs<TState>.I; } } private static readonly Lazy<ThreadPoolScheduler> LazyDefault = new Lazy<ThreadPoolScheduler>(() => new ThreadPoolScheduler()); [Obsolete("Use the Instance property", false)] [EditorBrowsable(EditorBrowsableState.Never)] public static ThreadPoolScheduler Default { get { return LazyDefault.Value; } } public static ThreadPoolScheduler Instance => LazyDefault.Value; public WorkItemPriority Priority { get; } public WorkItemOptions Options { get; } public ThreadPoolScheduler() { } public ThreadPoolScheduler(WorkItemPriority priority) { Priority = priority; Options = 0; } public ThreadPoolScheduler(WorkItemPriority priority, WorkItemOptions options) { Priority = priority; Options = options; } public unsafe override IDisposable Schedule<TState>(TState state, Func<IScheduler, TState, IDisposable> action) { if (action == null) throw new ArgumentNullException("action"); UserWorkItem<TState> userWorkItem = (UserWorkItem<TState>)new UserWorkItem<TState>((IScheduler)this, state, action); object obj; IAsyncAction asyncInfo = ThreadPool.RunAsync(new WorkItemHandler(obj, (IntPtr)(void*)), Priority, Options); ((UserWorkItem<TState>)userWorkItem).CancelQueueDisposable = ThreadPoolTimerExtensions.AsDisposable(asyncInfo); return userWorkItem; } public override IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Func<IScheduler, TState, IDisposable> action) { if (action == null) throw new ArgumentNullException("action"); TimeSpan dueTime2 = Scheduler.Normalize(dueTime); if (dueTime2.Ticks == 0) return Schedule(state, action); return ScheduleSlow(state, dueTime2, action); } private unsafe IDisposable ScheduleSlow<TState>(TState state, TimeSpan dueTime, Func<IScheduler, TState, IDisposable> action) { UserWorkItem<TState> userWorkItem = (UserWorkItem<TState>)new UserWorkItem<TState>((IScheduler)this, state, action); object obj; ThreadPoolTimer threadPoolTimer = ThreadPoolTimer.CreateTimer(new TimerElapsedHandler(obj, (IntPtr)(void*)), dueTime); ((UserWorkItem<TState>)userWorkItem).CancelQueueDisposable = threadPoolTimer.AsDisposable(); return userWorkItem; } public IDisposable SchedulePeriodic<TState>(TState state, TimeSpan period, Func<TState, TState> action) { if (period < TimeSpan.FromMilliseconds(1)) throw new ArgumentOutOfRangeException("period", Strings_PlatformServices.WINRT_NO_SUB1MS_TIMERS); if (action == null) throw new ArgumentNullException("action"); return new PeriodicallyScheduledWorkItem<TState>(state, period, action); } } }