3 回答

TA贡献1884条经验 获得超4个赞
您可以考虑使用超时运算符 。唯一的缺点是它以错误信号终止。您可能需要处理错误
超时运算符允许您中止具有 onError 终止的可观察对象,如果该可观察对象在指定的时间跨度内未能发出任何项。
如果您使用以下方法,则可以超越错误
.Timeout(200, Promise.resolve(42));
另一个变体允许您指示超时切换到您指定的备份 Observable,而不是在触发超时条件时以错误终止。
characteristic.WhenNotificationReceived()
.Timeout(TimeSpan.FromSeconds(1))
.Subscribe(
onNext: result =>
{
....
rfidPlayer.OnNext(....);
},
onError: error =>
{
rfidPlayer.OnCompleted();
});

TA贡献1801条经验 获得超16个赞
由于例外情况,我发现使用起来很讨厌。Timeout
我更喜欢将值注入到可用于终止序列的序列中。例如,如果我的序列产生非负数,那么如果我注入一个我知道结束序列。-1
下面是一个示例:
从这个可观察量开始,它生成从 1 开始的幂 2,并且它还延迟生成每个值的挂起值的毫秒数。
Observable
.Generate(1, x => true, x => 2 * x, x => x, x => TimeSpan.FromMilliseconds(x))
所以1,2,4,8等,越来越慢。
现在我想停止这个序列,如果秒没有值,那么我可以这样做:3.0
.Select(x => Observable.Timer(TimeSpan.FromSeconds(3.0)).Select(y => -1).StartWith(x))
.Switch()
.TakeWhile(x => x >= 0)
如果我运行此序列,我将得到以下输出:
1
2
4
8
16
32
64
128
256
512
1024
2048
该序列即将生成,但它首先等待毫秒以产生该值 - 与此同时,触发并输出一个从而停止序列。40964096Observable.Timer(TimeSpan.FromSeconds(3.0))-1
此查询的关键部分是使用 。它通过仅订阅最新的外部可观察和取消订阅前一个外部可观察来获取并生成 a。SwitchIObservable<IObservable<T>>IObservable<T>
因此,在我的查询中,序列生成的每个新值都会停止并重新启动 .Timer
在你的例子中,你的可观察量看起来像这样:
characteristic
.WhenNotificationReceived()
.Select(result => BitConverter.ToString(result.Data).Replace("-", ""))
.Select(x => Observable.Timer(TimeSpan.FromSeconds(1.0)).Select(y => (string)null).StartWith(x))
.Switch()
.TakeWhile(x => x != null)
.Subscribe(rfidPlayer);

TA贡献1878条经验 获得超4个赞
下面是一个可以使用的自定义运算符,它是内置超时运算符顶部的薄层。TakeUntilTimeout
/// <summary>
/// Applies a timeout policy for each element in the observable sequence.
/// If the next element isn't received within the specified timeout duration
/// starting from its predecessor, the sequence terminates.
/// </summary>
public static IObservable<T> TakeUntilTimeout<T>(
this IObservable<T> source,
TimeSpan timeout)
{
return source.Timeout(timeout, Observable.Empty<T>());
}
- 3 回答
- 0 关注
- 115 浏览
添加回答
举报