@@ -6,17 +6,17 @@ public enum ProgressState
6
6
{
7
7
Unknown ,
8
8
Starting ,
9
- BeforeTaskWait ,
10
- AfterTaskWait ,
9
+ BeforeTaskAwait ,
10
+ AfterTaskAwait ,
11
11
Finished ,
12
12
Error ,
13
13
Cancelled
14
14
}
15
15
16
16
public ProgressState State { get ; private set ; } = ProgressState . Unknown ;
17
17
18
- public Action < MultiTask > ? ProcessFeedback { get ; init ; }
19
- public Action < Task > ? TaskFeedback { get ; init ; }
18
+ public Action < MultiTask > ? OnStateChange { get ; init ; }
19
+ public Action < Task > ? OnTaskAwaited { get ; init ; }
20
20
21
21
public MultiTask ( )
22
22
{
@@ -25,35 +25,34 @@ public MultiTask()
25
25
public async Task WhenAll ( IEnumerable < Task > tasks , CancellationToken ? cancellationToken = null )
26
26
{
27
27
State = ProgressState . Starting ;
28
- ProcessFeedback ? . Invoke ( this ) ;
28
+ OnStateChange ? . Invoke ( this ) ;
29
29
30
30
HashSet < Task > tasksToExecute = tasks . ToHashSet ( ) ;
31
31
List < Exception > errors = new List < Exception > ( ) ;
32
32
33
33
while ( tasksToExecute . Any ( ) )
34
34
{
35
- State = ProgressState . BeforeTaskWait ;
36
- ProcessFeedback ? . Invoke ( this ) ;
35
+ State = ProgressState . BeforeTaskAwait ;
36
+ OnStateChange ? . Invoke ( this ) ;
37
37
38
- Task ? taskFinished = null ;
39
38
try
40
39
{
41
40
cancellationToken ? . ThrowIfCancellationRequested ( ) ;
42
41
43
- taskFinished = await Task . WhenAny ( tasksToExecute ) ;
42
+ Task finishedTask = await Task . WhenAny ( tasksToExecute ) ;
44
43
45
- State = ProgressState . AfterTaskWait ;
46
- ProcessFeedback ? . Invoke ( this ) ;
47
- TaskFeedback ? . Invoke ( taskFinished ) ;
44
+ State = ProgressState . AfterTaskAwait ;
45
+ OnStateChange ? . Invoke ( this ) ;
46
+ OnTaskAwaited ? . Invoke ( finishedTask ) ;
48
47
49
- if ( taskFinished . Status is TaskStatus . Faulted or TaskStatus . Canceled )
48
+ if ( finishedTask . Status is TaskStatus . Faulted or TaskStatus . Canceled )
50
49
{
51
- tasksToExecute . Remove ( taskFinished ) ;
52
- throw taskFinished . Exception ?? new Exception ( $ "Task ended in { taskFinished . Status } status without exception details") ;
50
+ tasksToExecute . Remove ( finishedTask ) ;
51
+ throw finishedTask . Exception ?? new Exception ( $ "Task ended in { finishedTask . Status } status without exception details") ;
53
52
}
54
- else if ( taskFinished . Status == TaskStatus . RanToCompletion )
53
+ else if ( finishedTask . Status == TaskStatus . RanToCompletion )
55
54
{
56
- if ( ! tasksToExecute . Remove ( taskFinished ) )
55
+ if ( ! tasksToExecute . Remove ( finishedTask ) )
57
56
throw new InvalidOperationException ( "Task reported as finished... again!" ) ;
58
57
}
59
58
}
@@ -72,22 +71,22 @@ public async Task WhenAll(IEnumerable<Task> tasks, CancellationToken? cancellati
72
71
if ( errors . All ( ex => ex is TaskCanceledException ) )
73
72
{
74
73
State = ProgressState . Cancelled ;
75
- ProcessFeedback ? . Invoke ( this ) ;
74
+ OnStateChange ? . Invoke ( this ) ;
76
75
77
76
throw new TaskCanceledException ( $ "All { errors . Count } tasks have been cancelled", new AggregateException ( errors ) ) ;
78
77
}
79
78
else
80
79
{
81
80
State = ProgressState . Error ;
82
- ProcessFeedback ? . Invoke ( this ) ;
81
+ OnStateChange ? . Invoke ( this ) ;
83
82
84
83
throw new AggregateException ( errors ) ;
85
84
}
86
85
}
87
86
else
88
87
{
89
88
State = ProgressState . Finished ;
90
- ProcessFeedback ? . Invoke ( this ) ;
89
+ OnStateChange ? . Invoke ( this ) ;
91
90
}
92
91
}
93
92
}
0 commit comments