| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
|
| 3 |
using System.Reactive.Concurrency; |
| 4 |
using System.Windows.Forms; |
| 5 |
|
| 6 |
namespace System.Reactive.Linq |
| 7 |
{ |
| 8 |
/// <summary> |
| 9 |
/// Provides a set of static methods for subscribing to IObservables using Windows Forms controls. |
| 10 |
/// </summary> |
| 11 |
public static class ControlObservable |
| 12 |
{ |
| 13 |
/// <summary> |
| 14 |
/// Wraps the source sequence in order to run its subscription and unsubscription logic on the Windows Forms message loop associated with the specified control. |
| 15 |
/// </summary> |
| 16 |
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam> |
| 17 |
/// <param name="source">Source sequence.</param> |
| 18 |
/// <param name="control">Windows Forms control whose associated message loop is used to to perform subscription and unsubscription actions on.</param> |
| 19 |
/// <returns>The source sequence whose subscriptions and unsubscriptions happen on the Windows Forms message loop associated with the specified control.</returns> |
| 20 |
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="control"/> is null.</exception> |
| 21 |
/// <remarks> |
| 22 |
/// Only the side-effects of subscribing to the source sequence and disposing subscriptions to the source sequence are run on the specified control. |
| 23 |
/// In order to invoke observer callbacks on the specified control, e.g. to render results in a control, use <see cref="ControlObservable.ObserveOn"/>. |
| 24 |
/// </remarks> |
| 25 |
public static IObservable<TSource> SubscribeOn<TSource>(this IObservable<TSource> source, Control control) |
| 26 |
{ |
| 27 |
if (source == null) |
| 28 |
throw new ArgumentNullException("source"); |
| 29 |
if (control == null) |
| 30 |
throw new ArgumentNullException("control"); |
| 31 |
|
| 32 |
return Synchronization.SubscribeOn(source, new ControlScheduler(control)); |
| 33 |
} |
| 34 |
|
| 35 |
/// <summary> |
| 36 |
/// Wraps the source sequence in order to run its observer callbacks on the Windows Forms message loop associated with the specified control. |
| 37 |
/// </summary> |
| 38 |
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam> |
| 39 |
/// <param name="source">Source sequence.</param> |
| 40 |
/// <param name="control">Windows Forms control whose associated message loop is used to to notify observers on.</param> |
| 41 |
/// <returns>The source sequence whose observations happen on the Windows Forms message loop associated with the specified control.</returns> |
| 42 |
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="control"/> is null.</exception> |
| 43 |
public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, Control control) |
| 44 |
{ |
| 45 |
if (source == null) |
| 46 |
throw new ArgumentNullException("source"); |
| 47 |
if (control == null) |
| 48 |
throw new ArgumentNullException("control"); |
| 49 |
|
| 50 |
return Synchronization.ObserveOn(source, new ControlScheduler(control)); |
| 51 |
} |
| 52 |
} |
| 53 |
} |