/* * Process Hacker - * provider base class * * Copyright (C) 2008-2009 wj32 * * This file is part of Process Hacker. * * Process Hacker is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Process Hacker is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Process Hacker. If not, see . */ using System; using System.Collections.Generic; using ProcessHacker.Common; using ProcessHacker.Common.Objects; namespace ProcessHacker { /// /// Provides services for continuously updating data. /// public abstract class Provider : BaseObject, IProvider { /// /// A generic delegate which is used when updating the dictionary. /// public delegate void ProviderUpdateOnce(); /// /// Represents a handler called when a dictionary item is added. /// /// The added item. public delegate void ProviderDictionaryAdded(TValue item); /// /// Represents a handler called when a dictionary item is modified. /// /// The old item. /// The new item. public delegate void ProviderDictionaryModified(TValue oldItem, TValue newItem); /// /// Represents a handler called when a dictionary item is removed. /// /// The removed item. public delegate void ProviderDictionaryRemoved(TValue item); /// /// Represents a handler called when an error occurs while updating. /// /// The raised exception. public delegate void ProviderError(Exception ex); public new event Action Disposed; public event ProviderUpdateOnce BeforeUpdate; /// /// Occurs when the provider has been updated. /// public event ProviderUpdateOnce Updated; /// /// Occurs when the provider adds an item to the dictionary. /// public event ProviderDictionaryAdded DictionaryAdded; /// /// Occurs when the provider modifies an item in the dictionary. /// public event ProviderDictionaryModified DictionaryModified; /// /// Occurs when the provider removes an item from the dictionary. /// public event ProviderDictionaryRemoved DictionaryRemoved; /// /// Occurs when an exception is raised while updating. /// public event ProviderError Error; private string _name = string.Empty; private IDictionary _dictionary; private bool _disposing; private bool _boosting; private bool _busy; private bool _enabled; private readonly LinkedListEntry _listEntry; private ProviderThread _owner; private int _runCount; private bool _unregistering; /// /// Creates a new instance of the Provider class. /// protected Provider() : this(new Dictionary()) { } /// /// Creates a new instance of the Provider class, specifying a /// custom equality comparer. /// protected Provider(IEqualityComparer comparer) : this(new Dictionary(comparer)) { } /// /// Creates a new instance of the Provider class, specifying a /// custom instance. /// protected Provider(IDictionary dictionary) { if (dictionary == null) throw new ArgumentNullException("dictionary"); _dictionary = dictionary; _listEntry = new LinkedListEntry { Value = this }; } protected override void DisposeObject(bool disposing) { _disposing = true; if (this.Disposed != null) { try { this.Disposed(this); } catch (Exception ex) { Logging.Log(ex); } } } public string Name { get { return _name; } protected set { _name = value; if (_name == null) _name = string.Empty; } } public bool Boosting { get { return _boosting; } set { _boosting = value; } } /// /// Determines whether the provider is currently updating. /// public bool Busy { get { return _busy; } } /// /// Gets whether the provider is shutting down. /// protected bool Disposing { get { return _disposing; } } /// /// Determines whether the provider should update. /// public bool Enabled { get { return _enabled; } set { _enabled = value; } } public LinkedListEntry ListEntry { get { return _listEntry; } } public ProviderThread Owner { get { return _owner; } set { _owner = value; } } /// /// Gets the number of times this provider has updated. /// public int RunCount { get { return _runCount; } } public bool Unregistering { get { return _unregistering; } set { _unregistering = value; } } /// /// Gets the dictionary. /// public IDictionary Dictionary { get { return _dictionary; } protected set { _dictionary = value; } } /// /// Causes the provider to run immediately. /// public void Boost() { _owner.Boost(this); } /// /// Updates the provider. Do not call this function. /// public void Run() { // Bail out if we are disposing if (_disposing) { Logging.Log(Logging.Importance.Warning, "Provider (" + _name + "): RunOnce: currently disposing"); return; } _busy = true; { try { if (BeforeUpdate != null) BeforeUpdate(); } catch { } try { this.Update(); _runCount++; } catch (Exception ex) { if (Error != null) { try { Error(ex); } catch { } } else { Logging.Log(Logging.Importance.Error, ex.ToString()); } } try { if (Updated != null) Updated(); } catch { } } _busy = false; } protected void OnDictionaryAdded(TValue item) { if (this.DictionaryAdded != null) this.DictionaryAdded(item); } protected void OnDictionaryModified(TValue oldItem, TValue newItem) { if (this.DictionaryModified != null) this.DictionaryModified(oldItem, newItem); } protected void OnDictionaryRemoved(TValue item) { if (this.DictionaryRemoved != null) this.DictionaryRemoved(item); } protected virtual void Update() { } } }