Reactive Rx 2.0 EventLoopScheduler ObjectDisposedException after dispose

后端 未结 3 1359
無奈伤痛
無奈伤痛 2021-01-06 11:15

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

3条回答
  •  Happy的楠姐
    2021-01-06 11:45

    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.

提交回复
热议问题