@@ -8,8 +8,8 @@ Module Example
8
8
Dim tokenSource As New CancellationTokenSource()
9
9
Dim token As CancellationToken = tokenSource.Token
10
10
11
- ' Store references to the tasks so that we can wait on them and
12
- ' observe their status after cancellation.
11
+ ' Store references to the tasks so that we can wait on them and
12
+ ' observe their status after cancellation.
13
13
Dim t As Task
14
14
Dim tasks As New ConcurrentBag( Of Task)()
15
15
@@ -18,26 +18,26 @@ Module Example
18
18
Console.WriteLine( "To terminate the example, press 'c' to cancel and exit..." )
19
19
Console.WriteLine()
20
20
21
- ' Request cancellation of a single task when the token source is canceled.
22
- ' Pass the token to the user delegate, and also to the task so it can
21
+ ' Request cancellation of a single task when the token source is canceled.
22
+ ' Pass the token to the user delegate, and also to the task so it can
23
23
' handle the exception correctly.
24
24
t = Task.Factory.StartNew( Sub () DoSomeWork( 1 , token), token)
25
25
Console.WriteLine( "Task {0} executing" , t.Id)
26
26
tasks.Add(t)
27
27
28
- ' Request cancellation of a task and its children. Note the token is passed
29
- ' to (1) the user delegate and (2) as the second argument to StartNew, so
28
+ ' Request cancellation of a task and its children. Note the token is passed
29
+ ' to (1) the user delegate and (2) as the second argument to StartNew, so
30
30
' that the task instance can correctly handle the OperationCanceledException.
31
31
t = Task.Factory.StartNew( Sub ()
32
- ' Create some cancelable child tasks.
32
+ ' Create some cancelable child tasks.
33
33
Dim tc As Task
34
34
For i As Integer = 3 To 10
35
- ' For each child task, pass the same token
35
+ ' For each child task, pass the same token
36
36
' to each user delegate and to StartNew.
37
37
tc = Task.Factory.StartNew( Sub (iteration) DoSomeWork(iteration, token), i, token)
38
38
Console.WriteLine( "Task {0} executing" , tc.Id)
39
39
tasks.Add(tc)
40
- ' Pass the same token again to do work on the parent task.
40
+ ' Pass the same token again to do work on the parent task.
41
41
' All will be signaled by the call to tokenSource.Cancel below.
42
42
DoSomeWork( 2 , token)
43
43
Next
@@ -47,30 +47,29 @@ Module Example
47
47
Console.WriteLine( "Task {0} executing" , t.Id)
48
48
tasks.Add(t)
49
49
50
- ' Request cancellation from the UI thread.
50
+ ' Request cancellation from the UI thread.
51
51
Dim ch As Char = Console.ReadKey().KeyChar
52
52
If ch = "c"c Or ch = "C"c Then
53
53
tokenSource.Cancel()
54
54
Console.WriteLine(vbCrLf + "Task cancellation requested." )
55
55
56
- ' Optional: Observe the change in the Status property on the task.
57
- ' It is not necessary to wait on tasks that have canceled. However,
58
- ' if you do wait, you must enclose the call in a try-catch block to
59
- ' catch the TaskCanceledExceptions that are thrown. If you do
60
- ' not wait, no exception is thrown if the token that was passed to the
61
- ' StartNew method is the same token that requested the cancellation.
56
+ ' Optional: Observe the change in the Status property on the task.
57
+ ' It is not necessary to wait on tasks that have canceled. However,
58
+ ' if you do wait, you must enclose the call in a try-catch block to
59
+ ' catch the OperationCanceledExceptions that are thrown. If you do
60
+ ' not wait, no exception is thrown if the token that was passed to the
61
+ ' StartNew method is the same token that requested the cancellation.
62
62
End If
63
63
64
64
Try
65
65
Task.WaitAll(tasks.ToArray())
66
66
Catch e As AggregateException
67
67
Console.WriteLine()
68
68
Console.WriteLine( "AggregateException thrown with the following inner exceptions:" )
69
- ' Display information about each exception.
69
+ ' Display information about each exception.
70
70
For Each v In e.InnerExceptions
71
- If TypeOf v Is TaskCanceledException
72
- Console.WriteLine( " TaskCanceledException: Task {0}" ,
73
- DirectCast (v, TaskCanceledException).Task.Id)
71
+ If TypeOf v Is OperationCanceledException Then
72
+ Console.WriteLine( " The operation was canceled." )
74
73
Else
75
74
Console.WriteLine( " Exception: {0}" , v.GetType().Name)
76
75
End If
@@ -80,14 +79,14 @@ Module Example
80
79
tokenSource.Dispose()
81
80
End Try
82
81
83
- ' Display status of all tasks.
82
+ ' Display status of all tasks.
84
83
For Each t In tasks
85
84
Console.WriteLine( "Task {0} status is now {1}" , t.Id, t.Status)
86
85
Next
87
86
End Sub
88
87
89
88
Sub DoSomeWork( ByVal taskNum As Integer , ByVal ct As CancellationToken)
90
- ' Was cancellation already requested?
89
+ ' Was cancellation already requested?
91
90
If ct.IsCancellationRequested = True Then
92
91
Console.WriteLine( "Task {0} was cancelled before it got started." ,
93
92
taskNum)
@@ -96,13 +95,13 @@ Module Example
96
95
97
96
Dim maxIterations As Integer = 100
98
97
99
- ' NOTE!!! A "TaskCanceledException was unhandled
100
- ' by user code" error will be raised here if "Just My Code"
101
- ' is enabled on your computer. On Express editions JMC is
102
- ' enabled and cannot be disabled. The exception is benign.
103
- ' Just press F5 to continue executing your code.
98
+ ' NOTE!!! A "TaskCanceledException was unhandled
99
+ ' by user code" error will be raised here if "Just My Code"
100
+ ' is enabled on your computer. On Express editions JMC is
101
+ ' enabled and cannot be disabled. The exception is benign.
102
+ ' Just press F5 to continue executing your code.
104
103
For i As Integer = 0 To maxIterations
105
- ' Do a bit of work. Not too much.
104
+ ' Do a bit of work. Not too much.
106
105
Dim sw As New SpinWait()
107
106
For j As Integer = 0 To 100
108
107
sw.SpinOnce()
@@ -117,7 +116,7 @@ End Module
117
116
' The example displays output like the following:
118
117
' Press any key to begin tasks...
119
118
' To terminate the example, press 'c' to cancel and exit...
120
- '
119
+ '
121
120
' Task 1 executing
122
121
' Task 2 executing
123
122
' Task 3 executing
@@ -130,12 +129,12 @@ End Module
130
129
' Task cancellation requested.
131
130
' Task 2 cancelled
132
131
' Task 7 cancelled
133
- '
132
+ '
134
133
' AggregateException thrown with the following inner exceptions:
135
134
' TaskCanceledException: Task 2
136
135
' TaskCanceledException: Task 8
137
136
' TaskCanceledException: Task 7
138
- '
137
+ '
139
138
' Task 2 status is now Canceled
140
139
' Task 1 status is now RanToCompletion
141
140
' Task 8 status is now Canceled
0 commit comments