Observable.Concurrency.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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.Threading;
  4. namespace System.Reactive.Linq
  5. {
  6. public static partial class Observable
  7. {
  8. #region + ObserveOn +
  9. /// <summary>
  10. /// Wraps the source sequence in order to run its observer callbacks on the specified scheduler.
  11. /// </summary>
  12. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  13. /// <param name="source">Source sequence.</param>
  14. /// <param name="scheduler">Scheduler to notify observers on.</param>
  15. /// <returns>The source sequence whose observations happen on the specified scheduler.</returns>
  16. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="scheduler"/> is null.</exception>
  17. /// <remarks>
  18. /// This only invokes observer callbacks on a scheduler. In case the subscription and/or unsubscription actions have side-effects
  19. /// that require to be run on a scheduler, use <see cref="Observable.SubscribeOn{TSource}(IObservable{TSource}, IScheduler)"/>.
  20. /// </remarks>
  21. public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, IScheduler scheduler)
  22. {
  23. if (source == null)
  24. throw new ArgumentNullException("source");
  25. if (scheduler == null)
  26. throw new ArgumentNullException("scheduler");
  27. return s_impl.ObserveOn<TSource>(source, scheduler);
  28. }
  29. #if !NO_SYNCCTX
  30. /// <summary>
  31. /// Wraps the source sequence in order to run its observer callbacks on the specified synchronization context.
  32. /// </summary>
  33. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  34. /// <param name="source">Source sequence.</param>
  35. /// <param name="context">Synchronization context to notify observers on.</param>
  36. /// <returns>The source sequence whose observations happen on the specified synchronization context.</returns>
  37. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="context"/> is null.</exception>
  38. /// <remarks>
  39. /// This only invokes observer callbacks on a synchronization context. In case the subscription and/or unsubscription actions have side-effects
  40. /// that require to be run on a synchronization context, use <see cref="Observable.SubscribeOn{TSource}(IObservable{TSource}, SynchronizationContext)"/>.
  41. /// </remarks>
  42. public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, SynchronizationContext context)
  43. {
  44. if (source == null)
  45. throw new ArgumentNullException("source");
  46. if (context == null)
  47. throw new ArgumentNullException("context");
  48. return s_impl.ObserveOn<TSource>(source, context);
  49. }
  50. #endif
  51. #endregion
  52. #region + SubscribeOn +
  53. /// <summary>
  54. /// Wraps the source sequence in order to run its subscription and unsubscription logic on the specified scheduler. This operation is not commonly used;
  55. /// see the remarks section for more information on the distinction between SubscribeOn and ObserveOn.
  56. /// </summary>
  57. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  58. /// <param name="source">Source sequence.</param>
  59. /// <param name="scheduler">Scheduler to perform subscription and unsubscription actions on.</param>
  60. /// <returns>The source sequence whose subscriptions and unsubscriptions happen on the specified scheduler.</returns>
  61. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="scheduler"/> is null.</exception>
  62. /// <remarks>
  63. /// This only performs the side-effects of subscription and unsubscription on the specified scheduler. In order to invoke observer
  64. /// callbacks on a scheduler, use <see cref="Observable.ObserveOn{TSource}(IObservable{TSource}, IScheduler)"/>.
  65. /// </remarks>
  66. public static IObservable<TSource> SubscribeOn<TSource>(this IObservable<TSource> source, IScheduler scheduler)
  67. {
  68. if (source == null)
  69. throw new ArgumentNullException("source");
  70. if (scheduler == null)
  71. throw new ArgumentNullException("scheduler");
  72. return s_impl.SubscribeOn<TSource>(source, scheduler);
  73. }
  74. #if !NO_SYNCCTX
  75. /// <summary>
  76. /// Wraps the source sequence in order to run its subscription and unsubscription logic on the specified synchronization context. This operation is not commonly used;
  77. /// see the remarks section for more information on the distinction between SubscribeOn and ObserveOn.
  78. /// </summary>
  79. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  80. /// <param name="source">Source sequence.</param>
  81. /// <param name="context">Synchronization context to perform subscription and unsubscription actions on.</param>
  82. /// <returns>The source sequence whose subscriptions and unsubscriptions happen on the specified synchronization context.</returns>
  83. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="context"/> is null.</exception>
  84. /// <remarks>
  85. /// This only performs the side-effects of subscription and unsubscription on the specified synchronization context. In order to invoke observer
  86. /// callbacks on a synchronization context, use <see cref="Observable.ObserveOn{TSource}(IObservable{TSource}, SynchronizationContext)"/>.
  87. /// </remarks>
  88. public static IObservable<TSource> SubscribeOn<TSource>(this IObservable<TSource> source, SynchronizationContext context)
  89. {
  90. if (source == null)
  91. throw new ArgumentNullException("source");
  92. if (context == null)
  93. throw new ArgumentNullException("context");
  94. return s_impl.SubscribeOn<TSource>(source, context);
  95. }
  96. #endif
  97. #endregion
  98. #region + Synchronize +
  99. /// <summary>
  100. /// Synchronizes the observable sequence such that observer notifications cannot be delivered concurrently.
  101. /// This overload is useful to "fix" an observable sequence that exhibits concurrent callbacks on individual observers, which is invalid behavior for the query processor.
  102. /// </summary>
  103. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  104. /// <param name="source">Source sequence.</param>
  105. /// <returns>The source sequence whose outgoing calls to observers are synchronized.</returns>
  106. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  107. /// <remarks>
  108. /// It's invalid behavior - according to the observer grammar - for a sequence to exhibit concurrent callbacks on a given observer.
  109. /// This operator can be used to "fix" a source that doesn't conform to this rule.
  110. /// </remarks>
  111. public static IObservable<TSource> Synchronize<TSource>(this IObservable<TSource> source)
  112. {
  113. if (source == null)
  114. throw new ArgumentNullException("source");
  115. return s_impl.Synchronize<TSource>(source);
  116. }
  117. /// <summary>
  118. /// Synchronizes the observable sequence such that observer notifications cannot be delivered concurrently, using the specified gate object.
  119. /// This overload is useful when writing n-ary query operators, in order to prevent concurrent callbacks from different sources by synchronizing on a common gate object.
  120. /// </summary>
  121. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  122. /// <param name="source">Source sequence.</param>
  123. /// <param name="gate">Gate object to synchronize each observer call on.</param>
  124. /// <returns>The source sequence whose outgoing calls to observers are synchronized on the given gate object.</returns>
  125. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="gate"/> is null.</exception>
  126. public static IObservable<TSource> Synchronize<TSource>(this IObservable<TSource> source, object gate)
  127. {
  128. if (source == null)
  129. throw new ArgumentNullException("source");
  130. if (gate == null)
  131. throw new ArgumentNullException("gate");
  132. return s_impl.Synchronize<TSource>(source, gate);
  133. }
  134. #endregion
  135. }
  136. }