CoreDispatcherScheduler
Represents an object that schedules units of work on a CoreDispatcher.
using System.Reactive.Disposables;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading;
using Windows.Foundation;
using Windows.System;
using Windows.UI.Core;
namespace System.Reactive.Concurrency
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
[CLSCompliant(false)]
public sealed class CoreDispatcherScheduler : LocalScheduler, ISchedulerPeriodic
{
[System.Runtime.CompilerServices.Nullable(2)]
private DispatcherQueue _dispatcherQueue;
public static CoreDispatcherScheduler Current {
get {
CoreWindow forCurrentThread = CoreWindow.GetForCurrentThread();
if ((int)forCurrentThread == 0)
throw new InvalidOperationException(Strings_WindowsThreading.NO_WINDOW_CURRENT);
return new CoreDispatcherScheduler(forCurrentThread.get_Dispatcher());
}
}
public CoreDispatcher Dispatcher { get; }
public CoreDispatcherPriority Priority { get; }
public CoreDispatcherScheduler(CoreDispatcher dispatcher)
{
if (dispatcher == null)
throw new ArgumentNullException("dispatcher");
Dispatcher = dispatcher;
Priority = 0;
}
public CoreDispatcherScheduler(CoreDispatcher dispatcher, CoreDispatcherPriority priority)
{
if (dispatcher == null)
throw new ArgumentNullException("dispatcher");
Dispatcher = dispatcher;
Priority = priority;
}
public unsafe override IDisposable Schedule<[System.Runtime.CompilerServices.Nullable(2)] TState>(TState state, Func<IScheduler, TState, IDisposable> action)
{
if (action == null)
throw new ArgumentNullException("action");
SingleAssignmentDisposable d = new SingleAssignmentDisposable();
<>c__DisplayClass11_0<TState> <>c__DisplayClass11_;
IAsyncAction asyncInfo = Dispatcher.RunAsync(Priority, new DispatchedHandler((object)<>c__DisplayClass11_, (IntPtr)(void*)));
return StableCompositeDisposable.Create(d, ThreadPoolTimerExtensions.AsDisposable(asyncInfo));
}
private unsafe DispatcherQueue CreateDispatcherQueue()
{
if (_dispatcherQueue != null)
return _dispatcherQueue;
if (Dispatcher.get_HasThreadAccess()) {
_dispatcherQueue = DispatcherQueue.GetForCurrentThread();
return _dispatcherQueue;
}
WindowsRuntimeSystemExtensions.GetAwaiter(Dispatcher.RunAsync(1, new DispatchedHandler((object)this, (IntPtr)(void*)))).GetResult();
return _dispatcherQueue;
}
public override IDisposable Schedule<[System.Runtime.CompilerServices.Nullable(2)] 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<[System.Runtime.CompilerServices.Nullable(2)] TState>(TState state, TimeSpan dueTime, Func<IScheduler, TState, IDisposable> action)
{
MultipleAssignmentDisposable d = new MultipleAssignmentDisposable();
DispatcherQueueTimer timer = CreateDispatcherQueue().CreateTimer();
DispatcherQueueTimer val = timer;
DispatcherQueueTimer obj = val;
Func<TypedEventHandler<DispatcherQueueTimer, object>, EventRegistrationToken> func = obj.add_Tick;
DispatcherQueueTimer obj2 = val;
<>c__DisplayClass14_0<TState> <>c__DisplayClass14_;
WindowsRuntimeMarshal.AddEventHandler<TypedEventHandler<DispatcherQueueTimer, object>>(func, (Action<EventRegistrationToken>)obj2.remove_Tick, new TypedEventHandler<DispatcherQueueTimer, object>((object)<>c__DisplayClass14_, (IntPtr)(void*)));
timer.put_Interval(dueTime);
timer.Start();
d.Disposable = Disposable.Create(delegate {
DispatcherQueueTimer val2 = Interlocked.Exchange<DispatcherQueueTimer>(ref timer, null);
if (val2 != null) {
val2.Stop();
action = ((IScheduler s, TState t) => Disposable.Empty);
}
});
return d;
}
public unsafe IDisposable SchedulePeriodic<[System.Runtime.CompilerServices.Nullable(2)] TState>(TState state, TimeSpan period, Func<TState, TState> action)
{
if (period < TimeSpan.Zero)
throw new ArgumentOutOfRangeException("period");
if (action == null)
throw new ArgumentNullException("action");
DispatcherQueueTimer timer = CreateDispatcherQueue().CreateTimer();
DispatcherQueueTimer val = timer;
DispatcherQueueTimer obj = val;
Func<TypedEventHandler<DispatcherQueueTimer, object>, EventRegistrationToken> func = obj.add_Tick;
DispatcherQueueTimer obj2 = val;
<>c__DisplayClass15_0<TState> <>c__DisplayClass15_;
WindowsRuntimeMarshal.AddEventHandler<TypedEventHandler<DispatcherQueueTimer, object>>(func, (Action<EventRegistrationToken>)obj2.remove_Tick, new TypedEventHandler<DispatcherQueueTimer, object>((object)<>c__DisplayClass15_, (IntPtr)(void*)));
timer.put_Interval(period);
timer.Start();
return Disposable.Create(delegate {
DispatcherQueueTimer val2 = Interlocked.Exchange<DispatcherQueueTimer>(ref timer, null);
if (val2 != null) {
val2.Stop();
action = ((TState _) => _);
}
});
}
}
}