gpt4 book ai didi

c# - 如何为 Observable 设置处置操作?

转载 作者:行者123 更新时间:2023-11-30 19:41:02 25 4
gpt4 key购买 nike

我知道如何创建一个可观察对象并分配一个处理操作:

Observable.Create(o =>
{
// o.OnNext etc.
return Disposable.Create(() => { /* ... */ });
});

但现在我从查询语法中生成了一个可观察对象:

var observable = from x in otherObservable
select x;

如何为此类查询分配处理操作?

最佳答案

如果我理解正确,您希望在处理订阅时“链接”或“收听”。一种方法是使用 Finally IObservable<T> 的运营商,因此:

var ob = from x in Observable.Interval(TimeSpan.FromSeconds(1))
select x;

// Use Finally to create an intermediate IObservable
var disposeSub = ob.Finally(() => Console.WriteLine("disposed"));

// Subscribe to the intermediate observable instead the original one
var yourSub = disposeSub.Subscribe(Console.WriteLine);

// Wait for some numbers to print
Thread.Sleep(TimeSpan.FromSeconds(4));

// "disposed" will be written on the console at this point
yourSub.Dispose();

希望对您有所帮助!

关于c# - 如何为 Observable 设置处置操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21285095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com