Subscription disposal -- does it wait for subscription action to end?

130 views Asked by At
var sub = observable.Subscribe(data => my_action(data));

When I dispose the sub variable it will unsubscribe. Ok, but when doing so will it wait for my_action to end if it was already called?

Update 1: I ask for conscious decision, not some side-effects. Compare this to Threading.Timer -- in order to wait for action you have to call special Dispose method.

Update 2: I ask for waiting for action to end, not to cancel the action.

2

There are 2 answers

3
Phillip Ngan On BEST ANSWER

It depends. If the action and the dispose occur on the same thread, then the disposal will happen after the action completes. If the action runs on a different thread to the disposal then they occur independently and it is possible for the disposal to occur while action is executing.

2
Shlomo On

If your question is, will subscription-disposal ever cancel an action triggered by a prior observation, the answer is no.

EDIT:

To answer your clarified question, the short answer is no. Disposal is scheduled immediately. Depending on your thread/scheduler situation, it will then either be executed immediately, or executed when there's an availaible thread.