后处置无接收2.0 EventLoopScheduler的ObjectDisposedException [英] Reactive Rx 2.0 EventLoopScheduler ObjectDisposedException after dispose

查看:311
本文介绍了后处置无接收2.0 EventLoopScheduler的ObjectDisposedException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是接收2.0的EventLoopScheduler排队/序列化的工作。当我需要处置调度,如果有剩余的工作,我会收到未处理的ObjectDisposedException。这是预期的行为?

I'm using Rx 2.0's EventLoopScheduler to queue up / serialize work. When I need to dispose the scheduler, if there is remaining work, I will receive an unhandled ObjectDisposedException. Is this the expected behavior?

做作/样品code:

    EventLoopScheduler scheduler = new EventLoopScheduler();
    List<IDisposable> handles = new List<IDisposable>();

    for (int i = 0; i < 100; ++i)
    {
        var handle = Observable.Interval(TimeSpan.FromMilliseconds(1))
                               .ObserveOn(scheduler)
                               .Subscribe(Observer.Create<long>((x) => Thread.Sleep(1000)));

        handles.Add(handle);
    }

    Thread.Sleep(TimeSpan.FromSeconds(1));

    foreach (var handle in handles)
        handle.Dispose();

    scheduler.Dispose();
    Console.ReadLine();

堆栈跟踪:

   System.ObjectDisposedException
   at System.Reactive.Concurrency.EventLoopScheduler.Schedule[TState](TState state, TimeSpan dueTime, Func`3 action)
   at System.Reactive.Concurrency.LocalScheduler.Schedule[TState](TState state, Func`3 action)
   at System.Reactive.Concurrency.Scheduler.<>c__DisplayClass50`1.<InvokeRec1>b__4e(TState state2)
   at System.Reactive.ScheduledObserver`1.Run(Object state, Action`1 recurse)
   at System.Reactive.Concurrency.Scheduler.<>c__DisplayClass50`1.<InvokeRec1>b__4d(TState state1)
   at System.Reactive.Concurrency.Scheduler.InvokeRec1[TState](IScheduler scheduler, Pair`2 pair)
   at System.Reactive.Concurrency.ScheduledItem`2.InvokeCore()
   at System.Reactive.Concurrency.ScheduledItem`1.Invoke()
   at System.Reactive.Concurrency.EventLoopScheduler.Run()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

与此类似悬而未决的问题:<一href="http://stackoverflow.com/questions/13109054/rx2-0-objectdisposedexception-after-diposing-eventloopscheduler">RX2.0:的ObjectDisposedException后diposing EventLoopScheduler

推荐答案

是啊,我看到过,以及 - 我不认为有一种方法来驱赶出来的事件线程本身,而是可以做这样的事情:

Yeah, I've seen that before as well - I don't think there's a way to "flush out the event thread" per se, but you can do something like this:

EventLoopScheduler scheduler = new EventLoopScheduler();
var wrappedScheduler = scheduler.Catch<Exception>((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<long>((x) => Thread.Sleep(1000)));

    handles.Add(handle);
}

这篇关于后处置无接收2.0 EventLoopScheduler的ObjectDisposedException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆