Export (0) Print
Expand All

ChangeMonitor.Dispose Method

Releases all resources that are used by the current instance of the ChangeMonitor class.

Namespace:  System.Runtime.Caching
Assembly:  System.Runtime.Caching (in System.Runtime.Caching.dll)

public void Dispose()

Implements

IDisposable.Dispose()

ExceptionCondition
InvalidOperationException

Initialization is not complete in the derived change-monitor class that called the base Dispose method.

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.

NoteNote

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.

NoteNote

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.

.NET Framework

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2015 Microsoft