You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
After cancellation simply means chaining a event after previous task, and allowing access to previous task in the callback so you can do things conditionally.
366
353
354
+
<!--TODO: faulted example is not right, the task is not canceled correctly-->
Copy file name to clipboardExpand all lines: docs/document/Modern CSharp/docs/Parallel Programming/Task/Exception Handling.md
+16-7Lines changed: 16 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,14 +17,22 @@ Task task2 = Task.Run(() =>
17
17
// program was not terminated
18
18
```
19
19
20
+
## What Might be Thrown
21
+
22
+
<!--TODO:might throw TaskCanceledException in some cases, does OperationCancelledException thrown?-->
23
+
Exception can be thrown and catched from a task for the following scenarios:
24
+
25
+
-`AggregateException` can be thrown from:
26
+
-`task.Wait();`
27
+
-`Task.Wait*`
28
+
-`task.Result`
29
+
- Direct exception can be thrown from:
30
+
-`await Task.When*`
31
+
-`await task`
32
+
-`task.GetAwaiter().GetResult()`
20
33
21
34
## Catch in Statement
22
35
23
-
Exception can be thrown and catched from a task for the following scenarios:
24
-
-`await task;`
25
-
-`task.Wait();`
26
-
-`task.Result`
27
-
-`task.GetAwaiter().GetResult()`
28
36
29
37
Exception yield from tasks is **always** a composite exception `AggregateException`**unless the exception is `OperationCancelledException` and the task has cancelled.**
30
38
@@ -88,13 +96,14 @@ catch (AggregateException ex)
88
96
}
89
97
```
90
98
91
-
## Access Exception From Task
99
+
100
+
## Handle in Continued Tasks
92
101
93
102
Task object itself can hold an `AggregateException` as a property.
94
103
So you may handle them in a continued task or a post operation.
Copy file name to clipboardExpand all lines: docs/document/Modern CSharp/docs/Parallel Programming/Task/Task Status.md
+36-9Lines changed: 36 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,7 @@
1
1
# Task Status
2
2
3
+
**One should always access tasks status in either `try..catch` statement or continued tasks**
4
+
3
5
## Created
4
6
5
7
Status `Created` is assigned when on task creation but usually seen on tasks created from constructors since other creation methods always start implicitly
A task has been scheduled by scheduler but has not yet behun execution
18
+
A task has been scheduled by scheduler but has not yet begun execution
15
19
16
-
## RunToCompletion
20
+
## RanToCompletion
17
21
18
-
Implying a task has completed successfully.
19
-
A task ran to end or terminated by returing a value has status `RunToCompletion`
22
+
A task ran to end or terminated by `return` or ran to end has status `RanToCompletion`
20
23
21
24
## Canceled
22
25
23
-
A successful cancellation happens **when all of the following were satisfied**
24
-
-`OperationCanceledException`(or its derived exception type) is thrown
26
+
A successful cancellation happens **when all of the three conditions were satisfied**
27
+
-`OperationCanceledException`(or its derived exception type such as `TaskCanceledException`) is thrown
25
28
-`token.IsCancellationRequested` is true
26
-
-`token` in closure passed to `OperationCanceledException` equals `token` as parameter
29
+
-`token` in closure passed to `OperationCanceledException` equals `token` as parameter on task creation
30
+
31
+
There's a special case that can result in Canceled status when a task requires unwrap.
32
+
If the inner task creation was Faulted because of a thrown of `OperationCanceledException`, the unwrap process would set the status of outer task to Canceled.
33
+
Otherwise it would just remain Faulted.
27
34
28
35
```cs
36
+
// compiler would choose a Task.Run(Func<Task> function) here
- Any exception besides `OperationCanceledException` was thrown.
49
73
-`OperationCanceledException` is thrown && (`token.IsCancellationRequested` is false || `token` in closure passed to `OperationCanceledException` != `token` as parameter)
74
+
- Any exception is not `OperationCanceledException` was thrown on task creation. A wait on the task created anyway results Faulted.
75
+
50
76
51
77
```cs
52
-
vartask=Task.Run(() =>
78
+
Tasktask=Task.Factory.StartNew(async() =>
53
79
{
80
+
awaitTask.Delay(100);
54
81
thrownewException(); // not an OperationCanceledException // [!code highlight]
0 commit comments