,System.TimeSpan)} ,System.TimeSpan,System.Reactive.Concurrency.IScheduler)} ,System.Func{--0,System.IObservable{--1}})}
Observable.Throttle(IObservable, TimeSpan) method¶
Defined in
Type: Observable
Namespace: System.Reactive.Linq
Assembly: System.Reactive.dll
Applies to
netstandard2.0
Overloads¶
- 1.
public static IObservable<TSource> Throttle<TSource>(this IObservable<TSource> source, TimeSpan dueTime) - 2.
public static IObservable<TSource> Throttle<TSource>(this IObservable<TSource> source, TimeSpan dueTime, IScheduler scheduler) - 3.
public static IObservable<TSource> Throttle<TSource, TThrottle>(this IObservable<TSource> source, Func<TSource, IObservable<TThrottle>> throttleDurationSelector)
1. Overload¶
public static IObservable<TSource> Throttle<TSource>(this IObservable<TSource> source, TimeSpan dueTime)
Summary: Ignores elements from an observable sequence which are followed by another element within a specified relative time duration.
Type parameters
| Name | Description |
|---|---|
TSource | The type of the elements in the source sequence. |
Parameters
| Name | Type | Description |
|---|---|---|
source | IObservable | Source sequence to throttle. |
dueTime | TimeSpan | Throttling duration for each element. |
Returns: IObservable
Remarks
This operator throttles the source sequence by holding on to each element for the duration specified in dueTime. If another
element is produced within this time window, the element is dropped and a new timer is started for the current element, repeating this whole
process. For streams that never have gaps larger than or equal to dueTime between elements, the resulting stream won't
produce any elements. In order to reduce the volume of a stream whilst guaranteeing the periodic production of elements, consider using the
Observable.Sample set of operators.
Specifying a TimeSpan.Zero value for dueTime is not recommended but supported, causing throttling timers to be scheduled
that are due immediately. However, this doesn't guarantee all elements will be retained in the result sequence. This is a side-effect of the
asynchrony introduced by the scheduler, where the action to forward the current element may not execute immediately, despite the TimeSpan.Zero
due time. In such cases, the next element may arrive before the scheduler gets a chance to run the throttling action.
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | source is null. |
| System.ArgumentOutOfRangeException | dueTime is less than TimeSpan.Zero. |
2. Overload¶
public static IObservable<TSource> Throttle<TSource>(this IObservable<TSource> source, TimeSpan dueTime, IScheduler scheduler)
Summary: Ignores elements from an observable sequence which are followed by another element within a specified relative time duration, using the specified scheduler to run throttling timers.
Type parameters
| Name | Description |
|---|---|
TSource | The type of the elements in the source sequence. |
Parameters
| Name | Type | Description |
|---|---|---|
source | IObservable | Source sequence to throttle. |
dueTime | TimeSpan | Throttling duration for each element. |
scheduler | [IScheduler](# | Scheduler to run the throttle timers on. |
Returns: IObservable
Remarks
This operator throttles the source sequence by holding on to each element for the duration specified in dueTime. If another
element is produced within this time window, the element is dropped and a new timer is started for the current element, repeating this whole
process. For streams that never have gaps larger than or equal to dueTime between elements, the resulting stream won't
produce any elements. In order to reduce the volume of a stream whilst guaranteeing the periodic production of elements, consider using the
Observable.Sample set of operators.
Specifying a TimeSpan.Zero value for dueTime is not recommended but supported, causing throttling timers to be scheduled
that are due immediately. However, this doesn't guarantee all elements will be retained in the result sequence. This is a side-effect of the
asynchrony introduced by the scheduler, where the action to forward the current element may not execute immediately, despite the TimeSpan.Zero
due time. In such cases, the next element may arrive before the scheduler gets a chance to run the throttling action.
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | source or scheduler is null. |
| System.ArgumentOutOfRangeException | dueTime is less than TimeSpan.Zero. |
3. Overload¶
public static IObservable<TSource> Throttle<TSource, TThrottle>(this IObservable<TSource> source, Func<TSource, IObservable<TThrottle>> throttleDurationSelector)
Summary: Ignores elements from an observable sequence which are followed by another value within a computed throttle duration.
Type parameters
| Name | Description |
|---|---|
TSource | The type of the elements in the source sequence. |
TThrottle | The type of the elements in the throttle sequences selected for each element in the source sequence. |
Parameters
| Name | Type | Description |
|---|---|---|
source | IObservable | Source sequence to throttle. |
throttleDurationSelector | Func | Selector function to retrieve a sequence indicating the throttle duration for each given element. |
Returns: IObservable
Remarks
This operator throttles the source sequence by holding on to each element for the duration denoted by throttleDurationSelector.
If another element is produced within this time window, the element is dropped and a new timer is started for the current element, repeating this
whole process. For streams where the duration computed by applying the throttleDurationSelector to each element overlaps with
the occurrence of the successor element, the resulting stream won't produce any elements. In order to reduce the volume of a stream whilst
guaranteeing the periodic production of elements, consider using the Observable.Sample set of operators.
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | source or throttleDurationSelector is null. |