Skip to content

Instantly share code, notes, and snippets.

@RolandPheasant
Created July 11, 2015 16:48
Show Gist options
  • Save RolandPheasant/b039b167605a457d9e07 to your computer and use it in GitHub Desktop.
Save RolandPheasant/b039b167605a457d9e07 to your computer and use it in GitHub Desktop.
Customer dispatcher scheduler
public class MyDispatcherScheduler: IScheduler
{
private readonly DispatcherScheduler _dispatcherScheduler;
private readonly ImmediateScheduler _immediateScheduler = ImmediateScheduler.Instance;
public MyDispatcherScheduler(DispatcherScheduler original)
{
_dispatcherScheduler = original;
}
public IDisposable Schedule<TState>(TState state, Func<IScheduler, TState, IDisposable> action)
{
return GetScheduler().Schedule(state, action);
}
public IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Func<IScheduler, TState, IDisposable> action)
{
return GetScheduler().Schedule(state,dueTime, action);
}
public IDisposable Schedule<TState>(TState state, DateTimeOffset dueTime, Func<IScheduler, TState, IDisposable> action)
{
return GetScheduler().Schedule(state, dueTime, action);
}
private IScheduler GetScheduler()
{
return _dispatcherScheduler.Dispatcher.CheckAccess() ? (IScheduler)_immediateScheduler : (IScheduler)_dispatcherScheduler;
}
public DateTimeOffset Now { get { return _dispatcherScheduler.Now; } }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment