-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathReportingTask.cs
More file actions
109 lines (94 loc) · 2.67 KB
/
Copy pathReportingTask.cs
File metadata and controls
109 lines (94 loc) · 2.67 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
using System;
using System.Threading;
using System.Threading.Tasks;
namespace ScrobbleMapper
{
/// <summary>
/// Adds progress tracking to TPL tasks
/// </summary>
class ReportingTask : ReportingTaskBase, IReportingTask
{
public Task Task { get; set; }
}
/// <summary>
/// Adds progress tracking to TPL futures
/// </summary>
class ReportingTask<T> : ReportingTaskBase, IReportingTask<T>
{
public Task<T> Task { get; set; }
Task IReportingTask.Task
{
get { return Task; }
}
}
/// <summary>
/// The immutable-ish interface to a reporting future
/// </summary>
interface IReportingTask<T> : IReportingTask
{
new Task<T> Task { get; }
}
/// <summary>
/// The immutable-ish interface to a reporting task
/// </summary>
interface IReportingTask : IDisposable
{
CancellationTokenSource CancellationTokenSource { get; }
Task Task { get; }
event Action ProgressChanged;
event Action DescriptionChanged;
float Progress { get; }
string Description { get; }
}
/// <summary>
/// Common code for reporting tasks and futures
/// </summary>
abstract class ReportingTaskBase
{
public event Action ProgressChanged = ActionUtil.NullAction;
public event Action DescriptionChanged = ActionUtil.NullAction;
public CancellationTokenSource CancellationTokenSource { get; set; } = new CancellationTokenSource();
public void ReportItemCompleted()
{
Interlocked.Increment(ref itemsCompleted);
ProgressChanged();
}
public void Dispose()
{
CancellationTokenSource.Dispose();
}
int itemsCompleted;
public int ItemsCompleted
{
get { return itemsCompleted; }
}
int totalItems;
public int TotalItems
{
get { return totalItems; }
set
{
totalItems = value;
ProgressChanged();
}
}
string description;
public string Description
{
get { return description; }
set
{
description = value;
DescriptionChanged();
}
}
public float Progress
{
get
{
int denominator = TotalItems == 0 ? 1 : TotalItems;
return (float)ItemsCompleted / denominator;
}
}
}
}