![](/img/trans.png)
[英]Throttle/Buffer one Observable collection based on another in Reactive Extensions
[英]Reactive Extensions - Pumping items from one collection to another
我有一個IEnumerable集合,並希望在一秒鍾內將物品泵入另一個集合。 我該如何實現這一目標? 有很多新的擴展方法。 我還沒有在哪里使用。
list1.ToObservable() // Convert list1 to Observable
.Zip(
Observable.Interval(TimeSpan.FromSeconds(1)), // Zip it with an observable that ticks every second
(list, timerList) => list // select list1 only
).
Subscribe((item) =>
{
list2.Add(item); // on each tick, add an item to list2
});
這是你可以做到的一種方式。 我不是說這是最“Rx”的方式,請注意(我對Rx的體驗仍然有限)。
public static IEnumerable<T> Delay(this IEnumerable<T> source, int interval)
{
foreach (T item in source)
{
Thread.Sleep(interval);
yield return item;
}
}
接着:
var source = firstCollection.Delay(1000).ToObservable();
source.Subscribe(x => secondCollection.Add(x));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.