// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT License.
// See the LICENSE file in the project root for more information.
#if LEGACY_WINRT
using System.ComponentModel;
using Windows.System.Threading;
namespace System.Reactive.Concurrency
{
///
/// Represents an object that schedules units of work on the Windows Runtime thread pool.
///
/// Singleton instance of this type exposed through this static property.
[CLSCompliant(false)]
public sealed class ThreadPoolScheduler : LocalScheduler, ISchedulerPeriodic
{
private static readonly Lazy LazyDefault = new(static () => new ThreadPoolScheduler());
///
/// Constructs a ThreadPoolScheduler that schedules units of work on the Windows ThreadPool.
///
public ThreadPoolScheduler()
{
}
///
/// Constructs a ThreadPoolScheduler that schedules units of work on the Windows ThreadPool with the given priority.
///
/// Priority for scheduled units of work.
public ThreadPoolScheduler(WorkItemPriority priority)
{
Priority = priority;
Options = WorkItemOptions.None;
}
///
/// Constructs a ThreadPoolScheduler that schedules units of work on the Windows ThreadPool with the given priority.
///
/// Priority for scheduled units of work.
/// Options that configure how work is scheduled.
public ThreadPoolScheduler(WorkItemPriority priority, WorkItemOptions options)
{
Priority = priority;
Options = options;
}
///
/// Gets the singleton instance of the Windows Runtime thread pool scheduler.
///
[Obsolete("Use the Instance property", false)]
[EditorBrowsable(EditorBrowsableState.Never)]
public static ThreadPoolScheduler Default => LazyDefault.Value;
///
/// Gets the singleton instance of the Windows Runtime thread pool scheduler.
///
public static ThreadPoolScheduler Instance => LazyDefault.Value;
///
/// Gets the priority at which work is scheduled.
///
public WorkItemPriority Priority { get; }
///
/// Gets the options that configure how work is scheduled.
///
public WorkItemOptions Options { get; }
///
/// Schedules an action to be executed.
///
/// The type of the state passed to the scheduled action.
/// State passed to the action to be executed.
/// Action to be executed.
/// The disposable object used to cancel the scheduled action (best effort).
/// is null.
public override IDisposable Schedule(TState state, Func action)
{
if (action == null)
throw new ArgumentNullException(nameof(action));
var userWorkItem = new UserWorkItem(this, state, action);
var res = ThreadPool.RunAsync(
iaa => userWorkItem.Run(),
Priority,
Options);
userWorkItem.CancelQueueDisposable = res.AsDisposable();
return userWorkItem;
}
///
/// Schedules an action to be executed after dueTime, using a Windows.System.Threading.ThreadPoolTimer object.
///
/// The type of the state passed to the scheduled action.
/// State passed to the action to be executed.
/// Action to be executed.
/// Relative time after which to execute the action.
/// The disposable object used to cancel the scheduled action (best effort).
/// is null.
public override IDisposable Schedule(TState state, TimeSpan dueTime, Func action)
{
if (action == null)
throw new ArgumentNullException(nameof(action));
var dt = Scheduler.Normalize(dueTime);
if (dt.Ticks == 0)
{
return Schedule(state, action);
}
return ScheduleSlow(state, dt, action);
}
private IDisposable ScheduleSlow(TState state, TimeSpan dueTime, Func action)
{
var userWorkItem = new UserWorkItem(this, state, action);
var res = ThreadPoolTimer.CreateTimer(
tpt => userWorkItem.Run(),
dueTime);
userWorkItem.CancelQueueDisposable = res.AsDisposable();
return userWorkItem;
}
///
/// Schedules a periodic piece of work, using a Windows.System.Threading.ThreadPoolTimer object.
///
/// The type of the state passed to the scheduled action.
/// Initial state passed to the action upon the first iteration.
/// Period for running the work periodically.
/// Action to be executed, potentially updating the state.
/// The disposable object used to cancel the scheduled recurring action (best effort).
/// is null.
/// is less than one millisecond.
public IDisposable SchedulePeriodic(TState state, TimeSpan period, Func 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));
return new PeriodicallyScheduledWorkItem(state, period, action);
}
private sealed class PeriodicallyScheduledWorkItem : IDisposable
{
private TState _state;
private Func _action;
private readonly ThreadPoolTimer _timer;
private readonly AsyncLock _gate = new();
public PeriodicallyScheduledWorkItem(TState state, TimeSpan period, Func action)
{
_state = state;
_action = action;
_timer = ThreadPoolTimer.CreatePeriodicTimer(
Tick,
period);
}
private void Tick(ThreadPoolTimer timer)
{
_gate.Wait(
this,
static @this => @this._state = @this._action(@this._state));
}
public void Dispose()
{
_timer.Cancel();
_gate.Dispose();
_action = Stubs.I;
}
}
}
}
#endif