1
0

ControlObservable.cs 3.1 KB

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