ChangeMonitor ChangeMonitor ChangeMonitor ChangeMonitor Class

定義

提供衍生之自訂型別的基底類別,這個型別會監控快取項目所依存之資料的狀態變更。Provides a base class for a derived custom type that monitors changes in the state of the data which a cache item depends on.

public ref class ChangeMonitor abstract : IDisposable
public abstract class ChangeMonitor : IDisposable
type ChangeMonitor = class
    interface IDisposable
Public MustInherit Class ChangeMonitor
Implements IDisposable
繼承
ChangeMonitorChangeMonitorChangeMonitorChangeMonitor
衍生
實作

備註

ChangeMonitor類別作為基底類別衍生的監控專門用於特定的快取實作的類別。The ChangeMonitor class is used as the base class for derived monitor classes that are specialized for particular cache implementations. AChangeMonitor執行個體監視的資料取決於快取項目狀態的變更。A ChangeMonitor instance monitors changes in the state of data which a cache item depends on. 例如,項目可以是檔案或資料的實體,其內容會快取。For example, the item can be a file or data entity whose contents are cached. 如果更新的檔案或資料的實體,則必須也變更相關聯的快取項目。If the file or data entity is updated, the associated cache entry must be changed also. 您可以監視的一般項目包括資料實體 (例如資料庫欄位、 值、 資料列或資料表)、 另一個快取項目,以及檔案和檔案屬性。Common items that can be monitored include data entities (such as database fields, values, rows, or tables), another cache entry, and files and file attributes.

相依性的狀態變更為相依性變更。A dependency change is a change in the state of a dependency. 在典型的快取實作中之後,ChangeMonitor執行個體通知快取相依性已變更時,快取會執行所需的動作,例如使插入快取項目失效。In a typical cache implementation, after a ChangeMonitor instance notifies the cache that a dependency has changed, the cache performs the required action, such as invalidating the inserted cache entry.

衍生ChangeMonitor可以自訂類別,以監視特定的不同資料存放區的相依性變更。Derived ChangeMonitor classes can be customized to monitor dependency changes for specific different data stores. 比方說,有針對檔案系統、 實體記憶體或資料庫的快取實作。For example, there are cache implementations for a file system, physical memory, or a database. ChangeMonitor類別會檢查外部快取中,會發生的變更,然後發出警示給快取中發生的變更。The ChangeMonitor class examines changes that occur outside the cache, and then alerts the cache that changes have occurred.

.NET Framework.NET Framework包括下列類別衍生自ChangeMonitor類別:The .NET Framework.NET Framework includes the following classes that derive from ChangeMonitor class:

每個類別適用於不同類型的相依性。Each of these classes works with different types of dependencies. 例如,衍生FileChangeMonitor類別監視快取項目取決於檔案系統資料 (檔案及資料夾) 的快取的變更。For example, the derived FileChangeMonitor class monitors changes to a cache for file system data (files and folders) that the cache item depends on.

給繼承者的注意事項

如果您建立自訂快取實作或衍生的變更監視類型,您必須遵循特定的指導方針。If you create a custom cache implementation or a derived change monitor type, you must follow certain guidelines. 下列清單摘要說明這些指導方針。The following list summarizes these guidelines. 如需詳細資訊,請參閱個別的方法或屬性的文件。For more information, see the documentation for individual methods or properties.

-在衍生類別的建構函式必須設定UniqueId屬性,開始監視,並呼叫InitializationComplete()方法傳回之前的方法。- The constructor of a derived class must set the UniqueId property, begin monitoring, and call InitializationComplete() method before the method returns. 如果建構函式在建構期間發生錯誤,而且必須具有要處置的資源,只能呼叫建構函式Dispose多載之後InitializationComplete()因為,方法呼叫Dispose多載會擲回InvalidOperationException如果初始化未完成的例外狀況。If a constructor encounters an error during constructions and must have to dispose of resources, the constructor can only call the Dispose overload after the InitializationComplete() method is called because the Dispose overload will throw an InvalidOperationException exception if initialization is not completed.

-如果發生在變更完成之前初始化為受監視的資料、 建構函式必須呼叫OnChanged(Object)方法之前呼叫InitializationComplete()方法。- If changes occur in the data that is being monitored before initialization is complete, the constructor must call the OnChanged(Object) method before calling the InitializationComplete() method.

-之後衍生ChangeMonitor型別會具現化,您必須插入自訂監視ObjectCache實作。- After a derived ChangeMonitor type is instantiated, you must insert the monitor into a custom ObjectCache implementation. 如果您使用完變更監視器,呼叫或Dispose方法。Or if you are finished using the change monitor, call the Dispose method.

-之後ChangeMonitor執行個體插入ObjectCache實作,ObjectCache執行個體所需變更監視器已處置。- After a ChangeMonitor instance is inserted into a ObjectCache implementation, the ObjectCache instance require that the change monitor is disposed. 插入無效,而且會導致例外狀況,即使ObjectCache實作必須呼叫Dispose多載。Even if the insertion is invalid and causes an exception, the ObjectCache implementation must call the Dispose overload.

之後衍生的變更監視器已插入快取中,ObjectCache實作必須呼叫NotifyOnChanged(OnChangedCallback),藉由傳遞OnChangedCallback物件。- After a derived change monitor is inserted into a cache, the ObjectCache implementation must call NotifyOnChanged(OnChangedCallback), by passing a OnChangedCallback object. NotifyOnChanged(OnChangedCallback)方法可以呼叫一次。The NotifyOnChanged(OnChangedCallback) method can be called only one time. 如果已經發生相依性變更,情況OnChangedCallback執行個體將會立即呼叫時NotifyOnChanged(OnChangedCallback)呼叫。If a dependency change has already occurred, the OnChangedCallback instance will be called immediately when NotifyOnChanged(OnChangedCallback) is called. 否則,OnChangedCallback執行個體將會呼叫一次。Otherwise, the OnChangedCallback instance will be called only one time. 此一次呼叫,都會發生時OnChanged(Object)呼叫方法,因為ChangeMonitor執行個體偵測到變更,或當Dispose()上呼叫方法ChangeMonitor,何者先發生。This one time call occurs either when the OnChanged(Object) method is called because the ChangeMonitor instance detected a change, or when the Dispose() method is called on the ChangeMonitor, whichever occurs first.

-OnChangedCallback所提供的執行個體ObjectCache實作應該移除相關聯的快取項目,並使用指定的原因DependencyChanged列舉型別。- The OnChangedCallback instance that is provided by an ObjectCache implementation should remove the associated cache entry and specify a reason by using the DependencyChanged enumeration.

-AChangeMonitor執行個體可以呼叫OnChanged(Object)方法是之前的快取實作呼叫NotifyOnChanged(OnChangedCallback)方法之後或。- A ChangeMonitor instance can call the OnChanged(Object) method either before a cache implementation has called NotifyOnChanged(OnChangedCallback) method or after. 如果OnChanged(Object)方法之前呼叫NotifyOnChanged(OnChangedCallback)呼叫時,基底ChangeMonitor實作會通知發生這種情況的快取,並且會觸發要傳遞至回呼NotifyOnChanged(OnChangedCallback)時,會立即NotifyOnChanged(OnChangedCallback)叫用。If the OnChanged(Object) method is called before NotifyOnChanged(OnChangedCallback) is called, the base ChangeMonitor implementation will notify the cache that this has occurred and will trigger the callback to be passed to NotifyOnChanged(OnChangedCallback) immediately when NotifyOnChanged(OnChangedCallback) is invoked. 傳遞給任何狀態資料OnChanged(Object)儲存變更監視器並接著傳遞給方法NotifyOnChanged(OnChangedCallback)方法時NotifyOnChanged(OnChangedCallback)叫用方法。Any state data that is passed to the OnChanged(Object) method is saved by the change monitor and subsequently passed to the NotifyOnChanged(OnChangedCallback) method when the NotifyOnChanged(OnChangedCallback) method is invoked.

-變更監視器必須實作Dispose(Boolean)方法。- A change monitor must implement the Dispose(Boolean) method. 如需詳細資訊,請參閱 Dispose(Boolean) 方法的文件。For more information, see the Dispose(Boolean) method documentation.

-Dispose()方法多載必須先呼叫處置ChangeMonitor執行個體。- The Dispose() method overload must be called to dispose of the ChangeMonitor instance. 呼叫 Dispose 的規則如下所示:The rules for calling Dispose are as follows: 項目插入快取之前,必須負責對 dispose 的呼叫端的ChangeMonitor執行個體。- Before an item is inserted into the cache, it is the responsibility of caller to dispose of a ChangeMonitor instance.

-一次快取項目和ChangeMonitor與它相關聯的執行個體傳遞至快取,快取實作器必須確定Dispose呼叫方法時,即使插入失敗。- Once cache item and the ChangeMonitor instances that are associated with it are passed to a cache, the cache implementer must make sure that the Dispose method is called, even if the insert fails.

之後的項目和其相關聯ChangeMonitor執行個體傳遞至快取、 呼叫端必須處置相依性,因為當Dispose呼叫方法,呼叫會被視為相依性已變更。- 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. 如此一來,OnChanged(Object)自動叫用方法。As a result, the OnChanged(Object) method is automatically invoked.

-納入考量,這些規則Dispose必須呼叫方法,透過下列方式之一:- Taking these rules into consideration, the Dispose method must be called in one of the following ways: -使用者必須先呼叫Dispose()如果他們決定不要插入快取中的衍生的變更監視器的執行個體的方法多載。- Users must call the Dispose() method overload if they decide not to insert the derived change-monitor instance into a cache.

-如果實作會嘗試將變更監控執行個體的物件快取插入,但插入失敗時,快取實作會負責呼叫Dispose()多載。- If the implementation tries to insert the change-monitor instance into an object cache but the insertion fails, the cache implementation is responsible for calling the Dispose() overload. 當插入嘗試會導致例外狀況時,快取實作必須處置任何相關聯的相依性。When the insertion attempt causes an exception, the cache implementation must dispose of any associated dependencies.

-如果移除快取項目時,快取實作也必須處置相依性。- If the cache entry is removed, the cache implementation must also dispose of the dependency.

-內部實作OnChanged(Object)方法會自動呼叫Dispose方法之後它會呼叫回呼,註冊透過NotifyOnChanged(OnChangedCallback)- The internal implementation of the OnChanged(Object) method automatically calls the Dispose method after it calls a callback that is registered through NotifyOnChanged(OnChangedCallback).

這個自動呼叫 dispose 方法,在事件引發時才會發生的初始化ChangeMonitor先前已完成執行個體。This automatic call to the dispose method during the event firing only occurs if the initialization of the ChangeMonitor instance was previously completed.

當衍生的變更監視器的建構函式呼叫InitializationComplete()方法,如果變更監視的狀態已經變更 (也就是建構函式仍在作用中時受到已變更的狀態) 則InitializationComplete()方法將自動處置變更監視器。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 already changed when the constructor was still active) then the InitializationComplete() method will automatically dispose of the change monitor.

-HasChanged屬性設定為true之後OnChanged(Object)方法會呼叫衍生的變更監視器執行個體,不論是否OnChangedCallback物件已由呼叫設定NotifyOnChanged(OnChangedCallback)方法與否。- The HasChanged property is set to true after the OnChanged(Object) method is called by the derived change-monitor instance, regardless of whether a OnChangedCallback object has been set by a call to the NotifyOnChanged(OnChangedCallback) method or not.

建構函式

ChangeMonitor() ChangeMonitor() ChangeMonitor() ChangeMonitor()

初始化 ChangeMonitor 類別的新執行個體。Initializes a new instance of the ChangeMonitor class. 這個建構函式由衍生類別中的建構函式呼叫,來初始化基底類別。This constructor is called from constructors in derived classes to initialize the base class.

屬性

HasChanged HasChanged HasChanged HasChanged

取得值,這個值表示 ChangeMonitor 類別所監控的狀態已變更。Gets a value that indicates that the state that is monitored by the ChangeMonitor class has changed.

IsDisposed IsDisposed IsDisposed IsDisposed

取得值,這個值表示是否已經處置 ChangeMonitor 類別的衍生執行個體。Gets a value that indicates that the derived instance of a ChangeMonitor class is disposed.

UniqueId UniqueId UniqueId UniqueId

取得值,這個值表示 ChangeMonitor 類別執行個體。Gets a value that represents the ChangeMonitor class instance.

方法

Dispose() Dispose() Dispose() Dispose()

ChangeMonitor 類別的目前執行個體所使用的資源全部釋出。Releases all resources that are used by the current instance of the ChangeMonitor class.

Dispose(Boolean) Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

釋放所有的 Managed 資源和 Unmanaged 資源,以及對 ChangeMonitor 執行個體的所有參考。Releases all managed and unmanaged resources and any references to the ChangeMonitor instance. 這項多載必須由衍生自變更監控的類別實作。This overload must be implemented by derived change-monitor classes.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
InitializationComplete() InitializationComplete() InitializationComplete() InitializationComplete()

從衍生類別的建構函式進行呼叫,以指出初始化已經完成。Called from the constructor of derived classes to indicate that initialization is finished.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
NotifyOnChanged(OnChangedCallback) NotifyOnChanged(OnChangedCallback) NotifyOnChanged(OnChangedCallback) NotifyOnChanged(OnChangedCallback)

在相依性變更時,由快取實作呼叫來註冊回呼,並透過 ObjectCache 委派通知 OnChangedCallback 執行個體。Called by Cache implementers to register a callback and notify an ObjectCache instance through the OnChangedCallback delegate when a dependency has changed.

OnChanged(Object) OnChanged(Object) OnChanged(Object) OnChanged(Object)

在相依性變更時由衍生類別呼叫,以引發事件。Called by derived classes to raise the event when a dependency changes.

ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

適用於

執行緒安全性

此型別具備執行緒安全。This type is thread safe.

另請參閱