//****************************************************************************************************** // ProcessProgressHandler.cs - Gbtc // // Copyright © 2012, Grid Protection Alliance. All Rights Reserved. // // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See // the NOTICE file distributed with this work for additional information regarding copyright ownership. // The GPA licenses this file to you under the MIT License (MIT), the "License"; you may // not use this file except in compliance with the License. You may obtain a copy of the License at: // // http://www.opensource.org/licenses/MIT // // Unless agreed to in writing, the subject software distributed under the License is distributed on an // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the // License for the specific language governing permissions and limitations. // // Code Modification History: // ---------------------------------------------------------------------------------------------------- // 11/04/2008 - J. Ritchie Carroll // Generated original version of source code. // 09/14/2009 - Stephen C. Wills // Added new header and license agreement. // 12/14/2012 - Starlynn Danyelle Gilliam // Modified Header. // //****************************************************************************************************** using System; namespace GSF { /// /// Defines a delegate handler for a instance. /// /// /// /// This handler is used by methods with an delegate parameter (e.g., Action<ProcessProgress<long>>) /// providing a simple callback mechanism for reporting progress on a long operation. /// /// /// Examples include: /// , /// and /// /// /// /// Unit of progress used (long, double, int, etc.) public class ProcessProgressHandler where TUnit : struct { #region [ Members ] // Fields private Action> m_progressHandler; private readonly ProcessProgress m_progressInstance; #endregion #region [ Constructors ] /// /// Constructs a new process progress handler for the specified parameters. /// /// Delegate callback to invoke as process progresses. /// Descriptive name of process, if useful. public ProcessProgressHandler(Action> progressHandler, string processName) { m_progressHandler = progressHandler; m_progressInstance = new ProcessProgress(processName); } /// /// Constructs a new process progress handler for the specified parameters. /// /// Delegate callback to invoke as process progresses. /// Descriptive name of process, if useful. /// Total number of units to be processed. public ProcessProgressHandler(Action> progressHandler, string processName, TUnit total) : this(progressHandler, processName) { m_progressInstance.Total = total; } #endregion #region [ Properties ] /// /// Gets instance of used to track progress for this handler. /// public ProcessProgress ProcessProgress { get { return m_progressInstance; } } /// /// Gets or sets reference to delegate handler used as a callback to report process progress. /// public Action> ProgressHandler { get { return m_progressHandler; } set { m_progressHandler = value; } } /// /// Gets or sets current process progress (i.e., number of units completed processing so far) - note that when this /// property value is assigned, the callback function is automatically called with updated /// instance so consumer can track progress. /// /// Number of units completed processing so far. public TUnit Complete { get { return m_progressInstance.Complete; } set { UpdateProgress(value); } } /// /// Gets or sets total number of units to be processed. /// public TUnit Total { get { return m_progressInstance.Total; } set { m_progressInstance.Total = value; } } #endregion #region [ Methods ] /// /// Calls callback function with updated instance so consumer can track progress. /// /// Number of units completed processing so far. /// /// Note that assigning a value to the property will have the same effect as calling this method. /// public void UpdateProgress(TUnit completed) { // Update bytes completed m_progressInstance.Complete = completed; // Call user function m_progressHandler(m_progressInstance); } #endregion } }