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 ObjectDis
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);
}
Actually, there is a simple way to flush the EventLoopScheduler
queue before disposing. First, make sure that all subscriptions that are adding actions to the queue are disposed, so that nothing else can be added.
Then, simply schedule the dispose of the scheduler itself:
scheduler.Schedule(() =>
{
// cleanup code: dispose of other stuff here
scheduler.Dispose();
});
This last scheduled action is ensured to run after all other pending actions have run and shutsdown the thread.
It's not that there is work remaining (i.e. the scheduler still has things in its queue), it's that you still have Subscriptions outstanding that are trying to add stuff to that queue.
Dispose all of the Subscriptions and then dispose the Scheduler, and it should work correctly