ControlObservable.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Reactive.Concurrency;
  5. using System.Windows.Forms;
  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 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(nameof(source));
  29. if (control == null)
  30. throw new ArgumentNullException(nameof(control));
  31. return Synchronization.SubscribeOn(source, new ControlScheduler(control));
  32. }
  33. /// <summary>
  34. /// Wraps the source sequence in order to run its observer callbacks on the Windows Forms message loop associated with the specified control.
  35. /// </summary>
  36. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  37. /// <param name="source">Source sequence.</param>
  38. /// <param name="control">Windows Forms control whose associated message loop is used to notify observers on.</param>
  39. /// <returns>The source sequence whose observations happen on the Windows Forms message loop associated with the specified control.</returns>
  40. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="control"/> is null.</exception>
  41. public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, Control control)
  42. {
  43. if (source == null)
  44. throw new ArgumentNullException(nameof(source));
  45. if (control == null)
  46. throw new ArgumentNullException(nameof(control));
  47. return Synchronization.ObserveOn(source, new ControlScheduler(control));
  48. }
  49. }
  50. }