|
1 | 1 | using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.Diagnostics.CodeAnalysis; |
2 | 4 | using System.Threading; |
| 5 | +using System.Threading.Tasks; |
3 | 6 |
|
4 | 7 | namespace ThreadingTest; |
5 | 8 |
|
6 | | -internal sealed class CancellationTokenTest |
| 9 | +public static class CancellationTokenTest |
7 | 10 | { |
8 | 11 |
|
9 | 12 | #region Constants & Statics |
10 | 13 |
|
11 | | - public static void Cancel_Test() |
| 14 | + public static async Task Cancel_OperationCanceledException_Test() |
| 15 | + { |
| 16 | + using var cts = new CancellationTokenSource(); |
| 17 | + var cancellToken = cts.Token; |
| 18 | + |
| 19 | + var task = Task.Factory |
| 20 | + .StartNew( |
| 21 | + () => |
| 22 | + { |
| 23 | + Thread.Sleep(1000); |
| 24 | + cancellToken.ThrowIfCancellationRequested(); |
| 25 | + Trace.WriteLine("end"); |
| 26 | + }, |
| 27 | + cancellToken); |
| 28 | + |
| 29 | + await Task.Delay(500); |
| 30 | + cts.Cancel(); |
| 31 | + |
| 32 | + try |
| 33 | + { |
| 34 | + await task; // throw OperationCanceledException |
| 35 | + |
| 36 | + //task.Wait(); // throw AggregateException TaskCanceledException |
| 37 | + //Task.WaitAll(new[] { task }); // throw AggregateException TaskCanceledException |
| 38 | + |
| 39 | + //await Task.WhenAll(task);// throw OperationCanceledException |
| 40 | + } |
| 41 | + catch (OperationCanceledException) |
| 42 | + { |
| 43 | + Console.WriteLine("OperationCanceledException"); |
| 44 | + } |
| 45 | + catch (AggregateException exception) |
| 46 | + { |
| 47 | + foreach (var inner in exception.InnerExceptions) |
| 48 | + { |
| 49 | + if (inner is TaskCanceledException) |
| 50 | + { |
| 51 | + Console.WriteLine("AggregateException TaskCanceledException"); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public static async Task Cancel_TaskCanceledException_Test() |
| 58 | + { |
| 59 | + using var cts = new CancellationTokenSource(); |
| 60 | + var cancellToken = cts.Token; |
| 61 | + |
| 62 | + cts.Cancel(true); |
| 63 | + |
| 64 | + var task = Task.Factory |
| 65 | + .StartNew( |
| 66 | + () => |
| 67 | + { |
| 68 | + Thread.Sleep(1000); |
| 69 | + cancellToken.ThrowIfCancellationRequested(); |
| 70 | + Trace.WriteLine("end"); |
| 71 | + }, |
| 72 | + cancellToken); |
| 73 | + |
| 74 | + try |
| 75 | + { |
| 76 | + await task; // throw TaskCanceledException |
| 77 | + |
| 78 | + //task.Wait(); // throw AggregateException TaskCanceledException |
| 79 | + //Task.WaitAll(new[] { task }); // throw AggregateException TaskCanceledException |
| 80 | + } |
| 81 | + catch (TaskCanceledException) |
| 82 | + { |
| 83 | + Console.WriteLine("TaskCanceledException"); |
| 84 | + } |
| 85 | + catch (AggregateException exception) |
| 86 | + { |
| 87 | + foreach (var inner in exception.InnerExceptions) |
| 88 | + { |
| 89 | + if (inner is TaskCanceledException) |
| 90 | + { |
| 91 | + Console.WriteLine("AggregateException TaskCanceledException"); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + [SuppressMessage("Usage", "CA2201:Do not raise reserved exception types", Justification = "<Pending>")] |
| 98 | + public static async Task Cancel_ThrowFirst_Test() |
12 | 99 | { |
13 | 100 | using var cts = new CancellationTokenSource(); |
14 | 101 | Console.WriteLine($"start: {Environment.CurrentManagedThreadId}"); |
15 | 102 |
|
16 | | - _ = cts.Token |
17 | | - .Register(() => |
| 103 | + var token = cts.Token; |
| 104 | + |
| 105 | + var t1 = Task.Run( |
| 106 | + async () => |
| 107 | + { |
| 108 | + Console.WriteLine($"run: 1-{Environment.CurrentManagedThreadId}"); |
| 109 | + while (!token.IsCancellationRequested) |
| 110 | + { |
| 111 | + await Task.Delay(100); |
| 112 | + } |
| 113 | + token.ThrowIfCancellationRequested(); |
| 114 | + }); |
| 115 | + |
| 116 | + var t2 = Task.Run( |
| 117 | + async () => |
18 | 118 | { |
19 | | - Console.WriteLine($"callback: {Environment.CurrentManagedThreadId}"); |
20 | | - Thread.Sleep(5000); |
| 119 | + Console.WriteLine($"run: 2-{Environment.CurrentManagedThreadId}"); |
| 120 | + while (!token.IsCancellationRequested) |
| 121 | + { |
| 122 | + await Task.Delay(100); |
| 123 | + } |
| 124 | + token.ThrowIfCancellationRequested(); |
21 | 125 | }); |
22 | 126 |
|
| 127 | + _ = cts.Token |
| 128 | + .Register( |
| 129 | + () => |
| 130 | + { |
| 131 | + Console.WriteLine($"callback: 1-{Environment.CurrentManagedThreadId}"); |
| 132 | + |
| 133 | + throw new Exception("1"); |
| 134 | + }); |
| 135 | + |
| 136 | + _ = cts.Token |
| 137 | + .Register( |
| 138 | + () => |
| 139 | + { |
| 140 | + Console.WriteLine($"callback: 2-{Environment.CurrentManagedThreadId}"); |
| 141 | + throw new Exception("2"); |
| 142 | + }); |
| 143 | + |
| 144 | + await Task.Delay(300); |
| 145 | + |
| 146 | + cts.Cancel(true); |
| 147 | + Console.WriteLine($"Canceled: {Environment.CurrentManagedThreadId}"); |
| 148 | + |
| 149 | + await Task.WhenAll(t1, t2); |
| 150 | + } |
| 151 | + |
| 152 | + public static void Register_Test() |
| 153 | + { |
| 154 | + using var cts = new CancellationTokenSource(); |
| 155 | + Console.WriteLine($"start: {Environment.CurrentManagedThreadId}"); |
| 156 | + |
| 157 | + _ = cts.Token |
| 158 | + .Register( |
| 159 | + () => |
| 160 | + { |
| 161 | + Console.WriteLine($"callback: {Environment.CurrentManagedThreadId}"); |
| 162 | + Thread.Sleep(5000); |
| 163 | + }); |
| 164 | + |
23 | 165 | Console.WriteLine($"Canceling: {Environment.CurrentManagedThreadId}"); |
24 | 166 | cts.Cancel(); |
25 | 167 | Console.WriteLine($"Canceled: {Environment.CurrentManagedThreadId}"); |
26 | 168 | } |
27 | 169 |
|
| 170 | + public static void Register_Twice_Test() |
| 171 | + { |
| 172 | + using var cts = new CancellationTokenSource(); |
| 173 | + Console.WriteLine($"start: {Environment.CurrentManagedThreadId}"); |
| 174 | + |
| 175 | + _ = cts.Token |
| 176 | + .Register( |
| 177 | + () => |
| 178 | + { |
| 179 | + Console.WriteLine($"callback: 1-{Environment.CurrentManagedThreadId}"); |
| 180 | + Thread.Sleep(1000); |
| 181 | + }); |
| 182 | + |
| 183 | + _ = cts.Token |
| 184 | + .Register( |
| 185 | + () => |
| 186 | + { |
| 187 | + Console.WriteLine($"callback: 2-{Environment.CurrentManagedThreadId}"); |
| 188 | + Thread.Sleep(1000); |
| 189 | + }); |
| 190 | + |
| 191 | + Console.WriteLine($"Canceling: {Environment.CurrentManagedThreadId}"); |
| 192 | + cts.Cancel(); |
| 193 | + Console.WriteLine($"Canceled: {Environment.CurrentManagedThreadId}"); |
| 194 | + } |
| 195 | + |
| 196 | + public static void Register_Twice_ThrowFirst_Test() |
| 197 | + { |
| 198 | + using var cts = new CancellationTokenSource(); |
| 199 | + Console.WriteLine($"start: {Environment.CurrentManagedThreadId}"); |
| 200 | + |
| 201 | + _ = cts.Token |
| 202 | + .Register( |
| 203 | + () => |
| 204 | + { |
| 205 | + Console.WriteLine($"callback: 1-{Environment.CurrentManagedThreadId}"); |
| 206 | + Thread.Sleep(1000); |
| 207 | + }); |
| 208 | + |
| 209 | + _ = cts.Token |
| 210 | + .Register( |
| 211 | + () => |
| 212 | + { |
| 213 | + Console.WriteLine($"callback: 2-{Environment.CurrentManagedThreadId}"); |
| 214 | + Thread.Sleep(1000); |
| 215 | + }); |
| 216 | + |
| 217 | + Console.WriteLine($"Canceling: {Environment.CurrentManagedThreadId}"); |
| 218 | + cts.Cancel(false); |
| 219 | + Console.WriteLine($"Canceled: {Environment.CurrentManagedThreadId}"); |
| 220 | + } |
| 221 | + |
28 | 222 | #endregion |
29 | 223 |
|
30 | 224 | } |
0 commit comments