RX2.0: ObjectDisposedException after diposing EventLoopScheduler

后端 未结 4 1582
有刺的猬
有刺的猬 2021-01-06 03:28

We have recently ported the system from RX 1.11111 to RX 2.0 and discovered this problem. We use an EventLoopScheduler for ObserveOn like this:

IDisposable s         


        
4条回答
  •  有刺的猬
    2021-01-06 03:49

    Just noticed this question as a link to this one: Reactive Rx 2.0 EventLoopScheduler ObjectDisposedException after dispose

    Shall repost here what I did there - I'm not aware of any way to "flush" the scheduler, but you can wrap/handle the inevitable "object disposed" exception this way:

    EventLoopScheduler scheduler = new EventLoopScheduler();
    var wrappedScheduler = scheduler.Catch((ex) => 
    {
        Console.WriteLine("Got an exception:" + ex.ToString());
        return true;
    });
    
    for (int i = 0; i < 100; ++i)
    {
        var handle = Observable.Interval(TimeSpan.FromMilliseconds(1))
                               .ObserveOn(wrappedScheduler)
                               .Subscribe(Observer.Create((x) => Thread.Sleep(1000)));
    
        handles.Add(handle);
    }
    

提交回复
热议问题