| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 | // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.#if !WINDOWSusing System.Reactive.Concurrency;using System.Windows;using System.Windows.Threading;namespace System.Reactive.Linq{    /// <summary>    /// Provides a set of extension methods for scheduling actions performed through observable sequences on UI dispatchers.    /// </summary>    public static class DispatcherObservable    {        #region ObserveOn[Dispatcher]        /// <summary>        /// Wraps the source sequence in order to run its observer callbacks on the specified dispatcher.        /// </summary>        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>        /// <param name="source">Source sequence.</param>        /// <param name="dispatcher">Dispatcher whose associated message loop is used to to notify observers on.</param>        /// <returns>The source sequence whose observations happen on the specified dispatcher.</returns>        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="dispatcher"/> is null.</exception>        public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, Dispatcher dispatcher)        {            if (source == null)                throw new ArgumentNullException("source");            if (dispatcher == null)                throw new ArgumentNullException("dispatcher");            return ObserveOn_<TSource>(source, dispatcher);        }#if HAS_DISPATCHER_PRIORITY        /// <summary>        /// Wraps the source sequence in order to run its observer callbacks on the specified dispatcher.        /// </summary>        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>        /// <param name="source">Source sequence.</param>        /// <param name="dispatcher">Dispatcher whose associated message loop is used to to notify observers on.</param>        /// <param name="priority">Priority to schedule work items at.</param>        /// <returns>The source sequence whose observations happen on the specified dispatcher.</returns>        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="dispatcher"/> is null.</exception>        public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, Dispatcher dispatcher, DispatcherPriority priority)        {            if (source == null)                throw new ArgumentNullException("source");            if (dispatcher == null)                throw new ArgumentNullException("dispatcher");            return ObserveOn_<TSource>(source, dispatcher, priority);        }#endif        /// <summary>        /// Wraps the source sequence in order to run its observer callbacks on the specified dispatcher scheduler.        /// </summary>        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>        /// <param name="source">Source sequence.</param>        /// <param name="scheduler">Dispatcher scheduler to notify observers on.</param>        /// <returns>The source sequence whose observations happen on the specified dispatcher scheduler.</returns>        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="scheduler"/> is null.</exception>        public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, DispatcherScheduler scheduler)        {            if (source == null)                throw new ArgumentNullException("source");            if (scheduler == null)                throw new ArgumentNullException("scheduler");#if HAS_DISPATCHER_PRIORITY            return ObserveOn_<TSource>(source, scheduler.Dispatcher, scheduler.Priority);#else            return ObserveOn_<TSource>(source, scheduler.Dispatcher);#endif        }#if USE_SL_DISPATCHER        /// <summary>        /// Wraps the source sequence in order to run its observer callbacks on the dispatcher associated with the specified object.        /// </summary>        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>        /// <param name="source">Source sequence.</param>        /// <param name="dependencyObject">Object to get the dispatcher from.</param>        /// <returns>The source sequence whose observations happen on the specified object's dispatcher.</returns>        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="dependencyObject"/> is null.</exception>        public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, DependencyObject dependencyObject)        {            if (source == null)                throw new ArgumentNullException("source");            if (dependencyObject == null)                throw new ArgumentNullException("dependencyObject");            return ObserveOn_<TSource>(source, dependencyObject.Dispatcher);        }#else        /// <summary>        /// Wraps the source sequence in order to run its observer callbacks on the dispatcher associated with the specified object.        /// </summary>        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>        /// <param name="source">Source sequence.</param>        /// <param name="dispatcherObject">Object to get the dispatcher from.</param>        /// <returns>The source sequence whose observations happen on the specified object's dispatcher.</returns>        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="dispatcherObject"/> is null.</exception>        public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, DispatcherObject dispatcherObject)        {            if (source == null)                throw new ArgumentNullException("source");            if (dispatcherObject == null)                throw new ArgumentNullException("dispatcherObject");            return ObserveOn_<TSource>(source, dispatcherObject.Dispatcher);        }#endif#if HAS_DISPATCHER_PRIORITY        /// <summary>        /// Wraps the source sequence in order to run its observer callbacks on the dispatcher associated with the specified object.        /// </summary>        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>        /// <param name="source">Source sequence.</param>        /// <param name="dispatcherObject">Object to get the dispatcher from.</param>        /// <param name="priority">Priority to schedule work items at.</param>        /// <returns>The source sequence whose observations happen on the specified object's dispatcher.</returns>        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="dispatcherObject"/> is null.</exception>        public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, DispatcherObject dispatcherObject, DispatcherPriority priority)        {            if (source == null)                throw new ArgumentNullException("source");            if (dispatcherObject == null)                throw new ArgumentNullException("dispatcherObject");            return ObserveOn_<TSource>(source, dispatcherObject.Dispatcher, priority);        }#endif        /// <summary>        /// Wraps the source sequence in order to run its observer callbacks on the dispatcher associated with the current thread.        /// </summary>        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>        /// <param name="source">Source sequence.</param>        /// <returns>The source sequence whose observations happen on the current thread's dispatcher.</returns>        /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>        public static IObservable<TSource> ObserveOnDispatcher<TSource>(this IObservable<TSource> source)        {            if (source == null)                throw new ArgumentNullException("source");#if USE_SL_DISPATCHER            return ObserveOn_<TSource>(source, System.Windows.Deployment.Current.Dispatcher);#else            return ObserveOn_<TSource>(source, DispatcherScheduler.Current.Dispatcher);#endif        }#if HAS_DISPATCHER_PRIORITY        /// <summary>        /// Wraps the source sequence in order to run its observer callbacks on the dispatcher associated with the current thread.        /// </summary>        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>        /// <param name="source">Source sequence.</param>        /// <param name="priority">Priority to schedule work items at.</param>        /// <returns>The source sequence whose observations happen on the current thread's dispatcher.</returns>        /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>        public static IObservable<TSource> ObserveOnDispatcher<TSource>(this IObservable<TSource> source, DispatcherPriority priority)        {            if (source == null)                throw new ArgumentNullException("source");            return ObserveOn_<TSource>(source, DispatcherScheduler.Current.Dispatcher, priority);        }        private static IObservable<TSource> ObserveOn_<TSource>(IObservable<TSource> source, Dispatcher dispatcher, DispatcherPriority priority)        {            return Synchronization.ObserveOn(source, new DispatcherSynchronizationContext(dispatcher, priority));        }#endif        private static IObservable<TSource> ObserveOn_<TSource>(IObservable<TSource> source, Dispatcher dispatcher)        {            return Synchronization.ObserveOn(source, new DispatcherSynchronizationContext(dispatcher));        }        #endregion        #region SubscribeOn[Dispatcher]        /// <summary>        /// Wraps the source sequence in order to run its subscription and unsubscription logic on the specified dispatcher.        /// </summary>        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>        /// <param name="source">Source sequence.</param>        /// <param name="dispatcher">Dispatcher whose associated message loop is used to to perform subscription and unsubscription actions on.</param>        /// <returns>The source sequence whose subscriptions and unsubscriptions happen on the specified dispatcher.</returns>        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="dispatcher"/> is null.</exception>        /// <remarks>        /// Only the side-effects of subscribing to the source sequence and disposing subscriptions to the source sequence are run on the specified dispatcher.        /// In order to invoke observer callbacks on the specified dispatcher, e.g. to render results in a control, use <see cref="DispatcherObservable.ObserveOn{TSource}(IObservable{TSource}, Dispatcher)"/>.        /// </remarks>        public static IObservable<TSource> SubscribeOn<TSource>(this IObservable<TSource> source, Dispatcher dispatcher)        {            if (source == null)                throw new ArgumentNullException("source");            if (dispatcher == null)                throw new ArgumentNullException("dispatcher");            return SubscribeOn_<TSource>(source, dispatcher);        }#if HAS_DISPATCHER_PRIORITY        /// <summary>        /// Wraps the source sequence in order to run its subscription and unsubscription logic on the specified dispatcher.        /// </summary>        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>        /// <param name="source">Source sequence.</param>        /// <param name="dispatcher">Dispatcher whose associated message loop is used to to perform subscription and unsubscription actions on.</param>        /// <param name="priority">Priority to schedule work items at.</param>        /// <returns>The source sequence whose subscriptions and unsubscriptions happen on the specified dispatcher.</returns>        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="dispatcher"/> is null.</exception>        /// <remarks>        /// Only the side-effects of subscribing to the source sequence and disposing subscriptions to the source sequence are run on the specified dispatcher.        /// In order to invoke observer callbacks on the specified dispatcher, e.g. to render results in a control, use <see cref="DispatcherObservable.ObserveOn{TSource}(IObservable{TSource}, Dispatcher, DispatcherPriority)"/>.        /// </remarks>        public static IObservable<TSource> SubscribeOn<TSource>(this IObservable<TSource> source, Dispatcher dispatcher, DispatcherPriority priority)        {            if (source == null)                throw new ArgumentNullException("source");            if (dispatcher == null)                throw new ArgumentNullException("dispatcher");            return SubscribeOn_<TSource>(source, dispatcher, priority);        }#endif        /// <summary>        /// Wraps the source sequence in order to run its subscription and unsubscription logic on the specified dispatcher scheduler.        /// </summary>        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>        /// <param name="source">Source sequence.</param>        /// <param name="scheduler">Dispatcher scheduler to perform subscription and unsubscription actions on.</param>        /// <returns>The source sequence whose subscriptions and unsubscriptions happen on the specified dispatcher scheduler.</returns>        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="scheduler"/> is null.</exception>        /// <remarks>        /// Only the side-effects of subscribing to the source sequence and disposing subscriptions to the source sequence are run on the specified scheduler.        /// In order to invoke observer callbacks on the specified scheduler, e.g. to render results in a control, use <see cref="DispatcherObservable.ObserveOn{TSource}(IObservable{TSource}, DispatcherScheduler)"/>.        /// </remarks>        public static IObservable<TSource> SubscribeOn<TSource>(this IObservable<TSource> source, DispatcherScheduler scheduler)        {            if (source == null)                throw new ArgumentNullException("source");            if (scheduler == null)                throw new ArgumentNullException("scheduler");#if HAS_DISPATCHER_PRIORITY            return SubscribeOn_<TSource>(source, scheduler.Dispatcher, scheduler.Priority);#else            return SubscribeOn_<TSource>(source, scheduler.Dispatcher);#endif        }#if USE_SL_DISPATCHER        /// <summary>        /// Wraps the source sequence in order to run its subscription and unsubscription logic on the dispatcher associated with the specified object.        /// </summary>        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>        /// <param name="source">Source sequence.</param>        /// <param name="dependencyObject">Object to get the dispatcher from.</param>        /// <returns>The source sequence whose subscriptions and unsubscriptions happen on the specified object's dispatcher.</returns>        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="dependencyObject"/> is null.</exception>        /// <remarks>        /// Only the side-effects of subscribing to the source sequence and disposing subscriptions to the source sequence are run on the dispatcher associated with the specified object.        /// In order to invoke observer callbacks on the dispatcher associated with the specified object, e.g. to render results in a control, use <see cref="DispatcherObservable.ObserveOn{TSource}(IObservable{TSource}, DependencyObject)"/>.        /// </remarks>        public static IObservable<TSource> SubscribeOn<TSource>(this IObservable<TSource> source, DependencyObject dependencyObject)        {            if (source == null)                throw new ArgumentNullException("source");            if (dependencyObject == null)                throw new ArgumentNullException("dependencyObject");            return SubscribeOn_<TSource>(source, dependencyObject.Dispatcher);        }#else        /// <summary>        /// Wraps the source sequence in order to run its subscription and unsubscription logic on the dispatcher associated with the specified object.        /// </summary>        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>        /// <param name="source">Source sequence.</param>        /// <param name="dispatcherObject">Object to get the dispatcher from.</param>        /// <returns>The source sequence whose subscriptions and unsubscriptions happen on the specified object's dispatcher.</returns>        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="dispatcherObject"/> is null.</exception>        /// <remarks>        /// Only the side-effects of subscribing to the source sequence and disposing subscriptions to the source sequence are run on the dispatcher associated with the specified object.        /// In order to invoke observer callbacks on the dispatcher associated with the specified object, e.g. to render results in a control, use <see cref="DispatcherObservable.ObserveOn{TSource}(IObservable{TSource}, DispatcherObject)"/>.        /// </remarks>        public static IObservable<TSource> SubscribeOn<TSource>(this IObservable<TSource> source, DispatcherObject dispatcherObject)        {            if (source == null)                throw new ArgumentNullException("source");            if (dispatcherObject == null)                throw new ArgumentNullException("dispatcherObject");            return SubscribeOn_<TSource>(source, dispatcherObject.Dispatcher);        }#endif#if HAS_DISPATCHER_PRIORITY        /// <summary>        /// Wraps the source sequence in order to run its subscription and unsubscription logic on the dispatcher associated with the specified object.        /// </summary>        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>        /// <param name="source">Source sequence.</param>        /// <param name="dispatcherObject">Object to get the dispatcher from.</param>        /// <param name="priority">Priority to schedule work items at.</param>        /// <returns>The source sequence whose subscriptions and unsubscriptions happen on the specified object's dispatcher.</returns>        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="dispatcherObject"/> is null.</exception>        /// <remarks>        /// Only the side-effects of subscribing to the source sequence and disposing subscriptions to the source sequence are run on the dispatcher associated with the specified object.        /// In order to invoke observer callbacks on the dispatcher associated with the specified object, e.g. to render results in a control, use <see cref="DispatcherObservable.ObserveOn{TSource}(IObservable{TSource}, DispatcherObject, DispatcherPriority)"/>.        /// </remarks>        public static IObservable<TSource> SubscribeOn<TSource>(this IObservable<TSource> source, DispatcherObject dispatcherObject, DispatcherPriority priority)        {            if (source == null)                throw new ArgumentNullException("source");            if (dispatcherObject == null)                throw new ArgumentNullException("dispatcherObject");            return SubscribeOn_<TSource>(source, dispatcherObject.Dispatcher, priority);        }#endif        /// <summary>        /// Wraps the source sequence in order to run its subscription and unsubscription logic on the dispatcher associated with the current thread.        /// </summary>        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>        /// <param name="source">Source sequence.</param>        /// <returns>The source sequence whose subscriptions and unsubscriptions happen on the current thread's dispatcher.</returns>        /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>        /// <remarks>        /// Only the side-effects of subscribing to the source sequence and disposing subscriptions to the source sequence are run on the dispatcher associated with the current thread.        /// In order to invoke observer callbacks on the dispatcher associated with the current thread, e.g. to render results in a control, use <see cref="DispatcherObservable.ObserveOnDispatcher{TSource}(IObservable{TSource})"/>.        /// </remarks>        public static IObservable<TSource> SubscribeOnDispatcher<TSource>(this IObservable<TSource> source)        {            if (source == null)                throw new ArgumentNullException("source");#if USE_SL_DISPATCHER            return SubscribeOn_<TSource>(source, System.Windows.Deployment.Current.Dispatcher);#else            return SubscribeOn_<TSource>(source, DispatcherScheduler.Current.Dispatcher);#endif        }#if HAS_DISPATCHER_PRIORITY        /// <summary>        /// Wraps the source sequence in order to run its subscription and unsubscription logic on the dispatcher associated with the current thread.        /// </summary>        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>        /// <param name="source">Source sequence.</param>        /// <param name="priority">Priority to schedule work items at.</param>        /// <returns>The source sequence whose observations happen on the current thread's dispatcher.</returns>        /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>        /// <remarks>        /// Only the side-effects of subscribing to the source sequence and disposing subscriptions to the source sequence are run on the dispatcher associated with the current thread.        /// In order to invoke observer callbacks on the dispatcher associated with the current thread, e.g. to render results in a control, use <see cref="DispatcherObservable.ObserveOnDispatcher{TSource}(IObservable{TSource}, DispatcherPriority)"/>.        /// </remarks>        public static IObservable<TSource> SubscribeOnDispatcher<TSource>(this IObservable<TSource> source, DispatcherPriority priority)        {            if (source == null)                throw new ArgumentNullException("source");            return SubscribeOn_<TSource>(source, DispatcherScheduler.Current.Dispatcher, priority);        }        private static IObservable<TSource> SubscribeOn_<TSource>(IObservable<TSource> source, Dispatcher dispatcher, DispatcherPriority priority)        {            return Synchronization.SubscribeOn(source, new DispatcherSynchronizationContext(dispatcher, priority));        }#endif        private static IObservable<TSource> SubscribeOn_<TSource>(IObservable<TSource> source, Dispatcher dispatcher)        {            return Synchronization.SubscribeOn(source, new DispatcherSynchronizationContext(dispatcher));        }        #endregion    }}#endif
 |