gpt4 book ai didi

c# - 处理后的 Reactive Rx 2.0 EventLoopScheduler ObjectDisposedException

转载 作者:太空狗 更新时间:2023-10-30 00:53:36 24 4
gpt4 key购买 nike

我正在使用 Rx 2.0 的 EventLoopScheduler 来排队/序列化工作。当我需要处理调度程序时,如果还有剩余工作,我将收到未处理的 ObjectDisposedException。这是预期的行为吗?

设计/示例代码:

    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()

类似于这个未回答的问题:RX2.0: ObjectDisposedException after diposing EventLoopScheduler

最佳答案

这并不是说还有剩余的工作(即调度程序的队列中仍然有东西),而是您仍然有未完成的订阅试图向该队列添加东西。

处理所有订阅,然后处理调度程序,它应该可以正常工作

关于c# - 处理后的 Reactive Rx 2.0 EventLoopScheduler ObjectDisposedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15675900/

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