我想了解如何將多個可觀察事件流式傳輸到Rx的一組事件中。但是當我運行下面的代碼時,我得到一個異常。那麼這是否意味着多個觀察者總是因爲違反Rx語法而容易出現異常?我的意思是,如果這些多個觀察者中的兩個偶然產生一個事件(任何兩個觀察者同時產生一些概率),它應該給出一個例外。觀察者可以安全地使用Rx監聽多個觀察對象嗎?
DateTimeOffset start;
object sync = new object();
var subject = new Subject<long>();
var observer = Observer.Create<long>(c =>
{
lock (sync)
{
Console.WriteLine(c);
}
})
;
var observable1 = Observable.Interval(TimeSpan.FromSeconds(2));
var observable2 = Observable.Interval(TimeSpan.FromSeconds(5));
var observable3 = Observable.Never<long>().Timeout
(start = DateTimeOffset.Now.AddSeconds(15),
(new long[] { 1 }).ToObservable());
var observable4 = Observable.Never<long>().Timeout(start);
observable1.Subscribe(observer);
observable2.Subscribe(observer);
observable3.Subscribe(observer);
observable4.Subscribe(observer);
Thread.Sleep(20000);
感謝Gideon的解釋。這是我得到的例外。你是對的,這是一個時間錯覺。這是一個編碼錯誤。謝謝。
System.TimeoutException: The operation has timed out.
at System.Reactive.Observer.<Create>b__8[T](Exception e)
at System.Reactive.AnonymousObserver`1.Error(Exception exception)
at System.Reactive.AbstractObserver`1.OnError(Exception error)
at System.Reactive.Subjects.Subject`1.OnError(Exception error)
at System.Reactive.AnonymousObservable`1.AutoDetachObserver.Error(Exception e
xception)
at System.Reactive.AbstractObserver`1.OnError(Exception error)
at System.Reactive.AnonymousObservable`1.AutoDetachObserver.Error(Exception e
xception)
at System.Reactive.AbstractObserver`1.OnError(Exception error)
at System.Reactive.Linq.Observable.<>c__DisplayClass28c`1.<>c__DisplayClass28
e.<Throw>b__28b()
at System.Reactive.Concurrency.Scheduler.Invoke(IScheduler scheduler, Action
action)
at System.Reactive.Concurrency.ImmediateScheduler.Schedule[TState](TState sta
te, Func`3 action)
at System.Reactive.Concurrency.Scheduler.Schedule(IScheduler scheduler, Actio
n action)
at System.Reactive.Linq.Observable.<>c__DisplayClass28c`1.<Throw>b__28a(IObse
rver`1 observer)
at System.Reactive.AnonymousObservable`1.<>c__DisplayClass1.<Subscribe>b__0()
at System.Reactive.Concurrency.Scheduler.Invoke(IScheduler scheduler, Action
action)
at System.Reactive.Concurrency.ScheduledItem`2.InvokeCore()
at System.Reactive.Concurrency.ScheduledItem`1.Invoke()
at System.Reactive.Concurrency.CurrentThreadScheduler.Trampoline.Run()
at System.Reactive.Concurrency.CurrentThreadScheduler.Schedule[TState](TState
state, TimeSpan dueTime, Func`3 action)
at System.Reactive.Concurrency.CurrentThreadScheduler.Schedule[TState](TState
state, Func`3 action)
at System.Reactive.Concurrency.Scheduler.Schedule(IScheduler scheduler, Actio
n action)
at System.Reactive.AnonymousObservable`1.Subscribe(IObserver`1 observer)
at System.Reactive.Linq.Observable.<>c__DisplayClass543`1.<>c__DisplayClass54
5.<Timeout>b__53f()
at System.Reactive.Concurrency.Scheduler.Invoke(IScheduler scheduler, Action
action)
at System.Reactive.Concurrency.ThreadPoolScheduler.<>c__DisplayClass8`1.<Sche
dule>b__6(Object _)
at System.Threading._TimerCallback.TimerCallback_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, C
ontextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading._TimerCallback.PerformTimerCallback(Object state)