如何有条件地停止可观察对象的定时间隔序列?

问题描述 投票:0回答:1

这是每秒运行以依次输出数字的代码:

IObservable<DateTimeOffset> timestamps =
    Observable.Interval(TimeSpan.FromSeconds(1))
    .Timestamp()
    .Where(x => x.Value % 2 == 0)
    .Select(x => x.Timestamp);
timestamps.Subscribe(x => Console.WriteLine(x));

有效。我的问题是,我希望Observable将生成此时间戳记,并在满足某些条件时停止,例如

if (x==100) then stop

是否有通过Rx / Linq实现此目的的方法?

非常感谢。

c# conditional-statements observable system.reactive intervals
1个回答
0
投票

“ Stop”是通知OnComplete。 “投掷”是通知OnError。有可用于推送这两种通知的运算符。

© www.soinside.com 2019 - 2024. All rights reserved.