// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. #if !NO_SYNCCTX using System.Reactive.Disposables; using System.Threading; namespace System.Reactive.Concurrency { /// /// Represents an object that schedules units of work on a provided . /// public class SynchronizationContextScheduler : LocalScheduler { private readonly SynchronizationContext _context; private readonly bool _alwaysPost; /// /// Creates an object that schedules units of work on the provided . /// /// Synchronization context to schedule units of work on. /// is null. public SynchronizationContextScheduler(SynchronizationContext context) { if (context == null) throw new ArgumentNullException("context"); _context = context; _alwaysPost = true; } /// /// Creates an object that schedules units of work on the provided . /// /// Synchronization context to schedule units of work on. /// Configures whether scheduling always posts to the synchronization context, regardless whether the caller is on the same synchronization context. /// is null. public SynchronizationContextScheduler(SynchronizationContext context, bool alwaysPost) { if (context == null) throw new ArgumentNullException("context"); _context = context; _alwaysPost = alwaysPost; } /// /// 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("action"); var d = new SingleAssignmentDisposable(); if (!_alwaysPost && _context == SynchronizationContext.Current) { d.Disposable = action(this, state); } else { _context.PostWithStartComplete(() => { if (!d.IsDisposed) d.Disposable = action(this, state); }); } return d; } /// /// Schedules an action to be executed after dueTime. /// /// 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("action"); var dt = Scheduler.Normalize(dueTime); if (dt.Ticks == 0) return Schedule(state, action); return DefaultScheduler.Instance.Schedule(state, dt, (_, state1) => Schedule(state1, action)); } } } #endif