The DisposeBase and AsyncDisposeBase classes provide a number of features that make it easier to write complex logic around the lifetimes of objects. They implement IDisposable and IAsyncDisposable respectively.
Override the CustomDispose or CustomDisposeAsync method to implement disposal behaviour that is guaranteed to execute only once.
-
The
DisposalReasonexception property allows inspection of the reason an object was disposed. -
The
DisposedTokencancellation token property allows tasks to be canceled when the object is disposed. -
The
DisposedTaskproperty allows user code to await disposal or be notified of disposal by adding a continuation to the task. -
The
KickoffDisposemethod allows you to start disposal in a threadpool thread without waiting for it to complete. Exceptions thrown by theCustomDisposemethod (due to a bug in YOUR code) are observed so that they don't get rethrown by the finalizer thread. -
The
IsDisposeStartedproperty allows you to check whether disposal has started, in a general sense. It's not perfectly thread-safe, so it could return false after another thread has entered the disposal method but not yet "flicked the switch" on the flag used by this property.
Neither of these base classes have a finalizer method. For this reason, the CustomDispose methods do not have a bool disposing parameter because it is assumed that Dispose is being called by using code, not by a Garbage Collector's finalizer thread.