Showing the changes to Source/Libraries/GSF.Core/ProcessProgressHandler.cs (1 of 628 files). Show all
Unified · Split
| @@ -0,0 +1,172 @@ | |||
| 1 | //****************************************************************************************************** | ||
| 2 | // ProcessProgressHandler.cs - Gbtc | ||
| 3 | // | ||
| 4 | // Copyright © 2010, Grid Protection Alliance. All Rights Reserved. | ||
| 5 | // | ||
| 6 | // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See | ||
| 7 | // the NOTICE file distributed with this work for additional information regarding copyright ownership. | ||
| 8 | // The GPA licenses this file to you under the Eclipse Public License -v 1.0 (the "License"); you may | ||
| 9 | // not use this file except in compliance with the License. You may obtain a copy of the License at: | ||
| 10 | // | ||
| 11 | // http://www.opensource.org/licenses/eclipse-1.0.php | ||
| 12 | // | ||
| 13 | // Unless agreed to in writing, the subject software distributed under the License is distributed on an | ||
| 14 | // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the | ||
| 15 | // License for the specific language governing permissions and limitations. | ||
| 16 | // | ||
| 17 | // Code Modification History: | ||
| 18 | // ---------------------------------------------------------------------------------------------------- | ||
| 19 | // 11/04/2008 - J. Ritchie Carroll | ||
| 20 | // Generated original version of source code. | ||
| 21 | // 09/14/2009 - Stephen C. Wills | ||
| 22 | // Added new header and license agreement. | ||
| 23 | // 10/8/2012 - Danyelle Gilliam | ||
| 24 | // Modified Header | ||
| 25 | // | ||
| 26 | //****************************************************************************************************** | ||
| 27 | |||
| 28 | |||
| 29 | |||
| 30 | |||
| 31 | |||
| 32 | using System; | ||
| 33 | |||
| 34 | namespace GSF | ||
| 35 | { | ||
| 36 | /// <summary> | ||
| 37 | /// Defines a delegate handler for a <see cref="GSF.ProcessProgress{TUnit}"/> instance. | ||
| 38 | /// </summary> | ||
| 39 | /// <remarks> | ||
| 40 | /// <para> | ||
| 41 | /// This handler is used by methods with an <see cref="Action"/> delegate parameter (e.g., Action<ProcessProgress<long>>) | ||
| 42 | /// providing a simple callback mechanism for reporting progress on a long operation. | ||
| 43 | /// </para> | ||
| 44 | /// <para> | ||
| 45 | /// Examples include: | ||
| 46 | /// <see cref="GSF.IO.Compression.CompressionExtensions.Compress(System.IO.Stream,System.IO.Stream,GSF.IO.Compression.CompressionStrength,Action{GSF.ProcessProgress{long}})"/>, | ||
| 47 | /// <see cref="GSF.Security.Cryptography.Cipher.Encrypt(System.IO.Stream,System.IO.Stream,byte[],byte[],GSF.Security.Cryptography.CipherStrength,Action{GSF.ProcessProgress{long}})"/> and | ||
| 48 | /// <see cref="GSF.Security.Cryptography.Cipher.Decrypt(System.IO.Stream,System.IO.Stream,byte[],byte[],GSF.Security.Cryptography.CipherStrength,Action{GSF.ProcessProgress{long}})"/> | ||
| 49 | /// </para> | ||
| 50 | /// </remarks> | ||
| 51 | /// <typeparam name="TUnit">Unit of progress used (long, double, int, etc.)</typeparam> | ||
| 52 | public class ProcessProgressHandler<TUnit> where TUnit : struct | ||
| 53 | { | ||
| 54 | #region [ Members ] | ||
| 55 | |||
| 56 | // Fields | ||
| 57 | private Action<ProcessProgress<TUnit>> m_progressHandler; | ||
| 58 | private ProcessProgress<TUnit> m_progressInstance; | ||
| 59 | |||
| 60 | #endregion | ||
| 61 | |||
| 62 | #region [ Constructors ] | ||
| 63 | |||
| 64 | /// <summary> | ||
| 65 | /// Constructs a new process progress handler for the specified parameters. | ||
| 66 | /// </summary> | ||
| 67 | /// <param name="progressHandler">Delegate callback to invoke as process progresses.</param> | ||
| 68 | /// <param name="processName">Descriptive name of process, if useful.</param> | ||
| 69 | public ProcessProgressHandler(Action<ProcessProgress<TUnit>> progressHandler, string processName) | ||
| 70 | { | ||
| 71 | m_progressHandler = progressHandler; | ||
| 72 | m_progressInstance = new ProcessProgress<TUnit>(processName); | ||
| 73 | } | ||
| 74 | |||
| 75 | /// <summary> | ||
| 76 | /// Constructs a new process progress handler for the specified parameters. | ||
| 77 | /// </summary> | ||
| 78 | /// <param name="progressHandler">Delegate callback to invoke as process progresses.</param> | ||
| 79 | /// <param name="processName">Descriptive name of process, if useful.</param> | ||
| 80 | /// <param name="total">Total number of units to be processed.</param> | ||
| 81 | public ProcessProgressHandler(Action<ProcessProgress<TUnit>> progressHandler, string processName, TUnit total) | ||
| 82 | : this(progressHandler, processName) | ||
| 83 | { | ||
| 84 | m_progressInstance.Total = total; | ||
| 85 | } | ||
| 86 | |||
| 87 | #endregion | ||
| 88 | |||
| 89 | #region [ Properties ] | ||
| 90 | |||
| 91 | /// <summary> | ||
| 92 | /// Gets instance of <see cref="GSF.ProcessProgress{TUnit}"/> used to track progress for this handler. | ||
| 93 | /// </summary> | ||
| 94 | public ProcessProgress<TUnit> ProcessProgress | ||
| 95 | { | ||
| 96 | get | ||
| 97 | { | ||
| 98 | return m_progressInstance; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | /// <summary> | ||
| 103 | /// Gets or sets reference to delegate handler used as a callback to report process progress. | ||
| 104 | /// </summary> | ||
| 105 | public Action<ProcessProgress<TUnit>> ProgressHandler | ||
| 106 | { | ||
| 107 | get | ||
| 108 | { | ||
| 109 | return m_progressHandler; | ||
| 110 | } | ||
| 111 | set | ||
| 112 | { | ||
| 113 | m_progressHandler = value; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | /// <summary> | ||
| 118 | /// Gets or sets current process progress (i.e., number of units completed processing so far) - note that when this | ||
| 119 | /// property value is assigned, the callback function is automatically called with updated <see cref="GSF.ProcessProgress{TUnit}"/> | ||
| 120 | /// instance so consumer can track progress. | ||
| 121 | /// </summary> | ||
| 122 | /// <value>Number of units completed processing so far.</value> | ||
| 123 | public TUnit Complete | ||
| 124 | { | ||
| 125 | get | ||
| 126 | { | ||
| 127 | return m_progressInstance.Complete; | ||
| 128 | } | ||
| 129 | set | ||
| 130 | { | ||
| 131 | UpdateProgress(value); | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | /// <summary> | ||
| 136 | /// Gets or sets total number of units to be processed. | ||
| 137 | /// </summary> | ||
| 138 | public TUnit Total | ||
| 139 | { | ||
| 140 | get | ||
| 141 | { | ||
| 142 | return m_progressInstance.Total; | ||
| 143 | } | ||
| 144 | set | ||
| 145 | { | ||
| 146 | m_progressInstance.Total = value; | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | #endregion | ||
| 151 | |||
| 152 | #region [ Methods ] | ||
| 153 | |||
| 154 | /// <summary> | ||
| 155 | /// Calls callback function with updated <see cref="GSF.ProcessProgress{TUnit}"/> instance so consumer can track progress. | ||
| 156 | /// </summary> | ||
| 157 | /// <param name="completed">Number of units completed processing so far.</param> | ||
| 158 | /// <remarks> | ||
| 159 | /// Note that assigning a value to the <see cref="Complete"/> property will have the same effect as calling this method. | ||
| 160 | /// </remarks> | ||
| 161 | public void UpdateProgress(TUnit completed) | ||
| 162 | { | ||
| 163 | // Update bytes completed | ||
| 164 | m_progressInstance.Complete = completed; | ||
| 165 | |||
| 166 | // Call user function | ||
| 167 | m_progressHandler(m_progressInstance); | ||
| 168 | } | ||
| 169 | |||
| 170 | #endregion | ||
| 171 | } | ||
| 172 | } | ||