ChangeMonitor.Dispose Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Releases all resources used by the current instance of the ChangeMonitor class.
Overloads
Dispose() |
Releases all resources that are used by the current instance of the ChangeMonitor class. |
Dispose(Boolean) |
Releases all managed and unmanaged resources and any references to the ChangeMonitor instance. This overload must be implemented by derived change-monitor classes. |
Remarks
The Dispose method is used to release the ChangeMonitor instance and related resources. The public Dispose method is invoked to coordinate the disposal process with key life-cycle events of derived change-monitor classes (such as initialization), and to release the ChangeMonitor instance so that the instance can be garbage collected. The Dispose method is implemented by derived change-monitor classes to dispose of their managed and unmanaged resources.
Dispose()
- Source:
- ChangeMonitor.cs
- Source:
- ChangeMonitor.cs
Releases all resources that are used by the current instance of the ChangeMonitor class.
public:
virtual void Dispose();
public void Dispose ();
abstract member Dispose : unit -> unit
override this.Dispose : unit -> unit
Public Sub Dispose ()
Implements
Exceptions
Initialization is not complete in the derived change-monitor class that called the base Dispose() method.
Remarks
The Dispose method invokes the Dispose method of derived classes only one time, the first time it is called. Subsequent calls to the Dispose method have no effect. After the method has been called, the IsDisposed property is set to true
.
The Dispose overload must be called to dispose of a ChangeMonitor instance. The following are the rules for calling the dispose method:
Before an item is inserted into the cache, it is the caller's responsibility to dispose of a ChangeMonitor instance.
Once cache item and the ChangeMonitor instances that are associated with it are passed to a cache, the cache implementer that must make sure that Dispose is called, even if the insert fails.
After an item and its associated ChangeMonitor instances are passed to a cache, the caller must not dispose the dependency because when the Dispose method is called, the call is treated as if the dependency has changed. As a result, the OnChanged method is automatically invoked.
Taking these rules into consideration, the Dispose method must be called in one of the following ways:
Users must call the Dispose method overload if they decide not to insert the derived change-monitor instance into a cache.
The cache implementation is responsible for calling the Dispose overload if the implementation tries to insert the change-monitor instance into an object cache but the insertion fails. When the insertion attempt causes an exception, the cache implementation must dispose any associated dependencies.
If the cache entry is removed, the cache implementation must also dispose the dependency.
The internal implementation of the OnChanged method automatically calls the Dispose method after it calls a callback that is registered through the NotifyOnChanged method.
Note
This automatic dispose during the event firing only occurs if the initialization of the ChangeMonitor instance previously completed.
When a derived change monitor's constructor calls the InitializationComplete method, if the state of the change monitor has already changed (that is, the state that is monitored has already changed when the constructor was still active) then InitializationComplete method will automatically dispose the change monitor.
Note
Users should not call the Dispose method. However, you cannot prevent users from calling the method. Therefore, if they do, the OnChanged method is invoked. In that case, the cache entry is notified as if the dependency has changed.
To prevent derived classes from overriding Dispose method, the method is not an explicit interface implementation.
See also
Applies to
Dispose(Boolean)
- Source:
- ChangeMonitor.cs
- Source:
- ChangeMonitor.cs
Releases all managed and unmanaged resources and any references to the ChangeMonitor instance. This overload must be implemented by derived change-monitor classes.
protected:
abstract void Dispose(bool disposing);
protected abstract void Dispose (bool disposing);
abstract member Dispose : bool -> unit
Protected MustOverride Sub Dispose (disposing As Boolean)
Parameters
- disposing
- Boolean
true
to release managed and unmanaged resources and any references to a ChangeMonitor instance; false
to release only unmanaged resources. When false
is passed, the Dispose(Boolean) method is called by a finalizer
thread and any external managed references are likely no longer valid because they have already been garbage collected.
Remarks
When the value of disposing
value is true
, all managed and unmanaged resources are disposed and any references to this object are released so that the derived change-monitor instance can be garbage collected. It is guaranteed that the base Dispose method will invoke the implemented Dispose method only one time.
Notes to Implementers
A change monitor must implement the Dispose(Boolean) overload to release all managed and unmanaged resources when the value of disposing
is true
. The Dispose(Boolean) method overload that has a disposing
value of true
is called only one time, namely, when the instance is disposed for the first time. A change monitor must not call the Dispose(Boolean) overload directly. A derived change monitor can call the public parameter-less Dispose() method on the base ChangeMonitor class.
Alternatively, a change monitor can implement a finalizer method. In that case, the finalizer can invoke the Dispose(Boolean) method and pass it a disposing
value of false
. However, this is usually unnecessary. Monitoring for dependency changes is typically performed by a service that maintains a reference to the change-monitor instance. The reference prevents the instance from being garbage collected, and therefore makes a finalizer method unnecessary. To avoid memory leaks, when a dependency changes, the OnChanged(Object) method disposes the change-monitor instance (unless initialization has not finished).