1
我想了解RX,並正在爲兩個不同的observables訂閱創建的觀察者。訂閱多個observable的觀察者未完成
var observer = Observer.Create<string>(x => Console.WriteLine(x),
() => Console.WriteLine("Completed"));
var subscription1 = Observable.Interval(TimeSpan.FromSeconds(1))
.Select(x => "X" + x)
.Subscribe(observer);
var subscription2 = Observable.Interval(TimeSpan.FromSeconds(2))
.Select(x => "YY" + x)
.Subscribe(observer);
Console.WriteLine("Unsubscribing in 5 seconds");
Thread.Sleep(5000);
subscription1.Dispose();
subscription2.Dispose();
Console.WriteLine("All disposed");
我認爲在處置訂閱1後,觀察者將完成。我在這裏錯過了什麼?目前,我得到以下控制檯輸出從上面的代碼:
Unsubscribing in 5 seconds
X0
YY0
X1
X2
YY1
X3
All disposed
謝謝!我完全錯過了取消訂閱意味着停止收聽的觀點。 :) – Rok