-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathProgressCalculator.cs
More file actions
115 lines (107 loc) · 4.88 KB
/
ProgressCalculator.cs
File metadata and controls
115 lines (107 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
namespace BytecodeApi.Mathematics;
/// <summary>
/// Represents a performance counter for progress calculations, typically used in binary transfers (file transferring, downloads, ...)
/// </summary>
public class ProgressCalculator
{
private DateTime LastMeasuredTime;
private double LastMeasuredValue;
/// <summary>
/// Gets or sets the value indicating the total progress. This is typically the amount of transferred bytes in a binary transfer operation.
/// </summary>
public double Value
{
get;
set
{
bool measured = false;
DateTime now = DateTime.Now;
if (now - LastMeasuredTime > MeasureTimeSpan)
{
LastMeasuredTime = now;
Difference = value - LastMeasuredValue;
LastMeasuredValue = value;
measured = true;
}
field = value;
ProgressCalculatorEventArgs e = new(Value, Progress, ProgressPercentage, Difference, DifferencePerSecond);
OnValueChanged(e);
if (measured)
{
OnMeasured(e);
}
}
}
/// <summary>
/// Gets or sets the expected maximum value. This is typically the total size in bytes in a binary transfer operation. If <see cref="MaxValue" /> is <see langword="null" />, values calculated based on it will also be <see langword="null" />. This is useful for display of an indeterminate status of progress bars.
/// </summary>
public double? MaxValue { get; set; }
/// <summary>
/// Gets or sets the interval in which <see cref="Value" /> is measured. The <see cref="Measured" /> event is raised in the approximate interval of this value, however only after <see cref="Value" /> has changed.
/// </summary>
public TimeSpan MeasureTimeSpan { get; set; }
/// <summary>
/// Gets a <see cref="double" /> value indicating the progress. This value is between 0.0 and 1.0, if <see cref="Value" /> is in bounds of 0.0 and <see cref="MaxValue" />.
/// </summary>
public double? Progress => MaxValue == null ? null : MaxValue == 0 ? 1 : Value / MaxValue.Value;
/// <summary>
/// Gets a <see cref="double" /> value indicating the progress in percent. This value is between 0.0 and 100.0, if <see cref="Value" /> is in bounds of 0.0 and <see cref="MaxValue" />.
/// </summary>
public double? ProgressPercentage => Progress * 100;
/// <summary>
/// Gets the difference of the <see cref="Value" /> property between the last two measurements.
/// </summary>
public double Difference { get; private set; }
/// <summary>
/// Gets the difference of the <see cref="Value" /> property between the last two measurements, interpolated to one second based on <see cref="MeasureTimeSpan" />.
/// </summary>
public double DifferencePerSecond => MeasureTimeSpan > TimeSpan.Zero ? Difference / MeasureTimeSpan.TotalSeconds : double.NaN;
/// <summary>
/// Occurs when the <see cref="Value" /> property changed.
/// </summary>
public event EventHandler<ProgressCalculatorEventArgs>? ValueChanged;
/// <summary>
/// Occurs when the <see cref="Value" /> property changed and the interval specified in <see cref="MeasureTimeSpan" /> has been reached.
/// </summary>
public event EventHandler<ProgressCalculatorEventArgs>? Measured;
/// <summary>
/// Initializes a new instance of the <see cref="ProgressCalculator" /> class with no maximum value and a measurement interval of 1 second.
/// </summary>
public ProgressCalculator()
{
MeasureTimeSpan = TimeSpan.FromSeconds(1);
}
/// <summary>
/// Initializes a new instance of the <see cref="ProgressCalculator" /> class with the specified maximum value and a measurement interval of 1 second.
/// </summary>
/// <param name="maxValue">A <see cref="double" /> value indicating the maximum expected value for <see cref="Value" />.</param>
public ProgressCalculator(double? maxValue) : this()
{
MaxValue = maxValue;
}
/// <summary>
/// Initializes a new instance of the <see cref="ProgressCalculator" /> class with the specified maximum value and the specified measurement interval.
/// </summary>
/// <param name="maxValue">A <see cref="double" /> value indicating the maximum expected value for <see cref="Value" />.</param>
/// <param name="measureTimeSpan">A <see cref="TimeSpan" /> value indicating the interval in which to measure the progress.</param>
public ProgressCalculator(double? maxValue, TimeSpan measureTimeSpan) : this(maxValue)
{
MeasureTimeSpan = measureTimeSpan;
}
/// <summary>
/// Raises the <see cref="ValueChanged" /> event.
/// </summary>
/// <param name="e">The event data for the <see cref="ValueChanged" /> event.</param>
protected virtual void OnValueChanged(ProgressCalculatorEventArgs e)
{
ValueChanged?.Invoke(this, e);
}
/// <summary>
/// Raises the <see cref="Measured" /> event.
/// </summary>
/// <param name="e">The event data for the <see cref="Measured" /> event.</param>
protected virtual void OnMeasured(ProgressCalculatorEventArgs e)
{
Measured?.Invoke(this, e);
}
}