@@ -39,7 +39,7 @@ internal static class TaskExtensions
39
39
{
40
40
#if ! NET6_0_OR_GREATER
41
41
private static readonly TaskContinuationOptions s_tco = TaskContinuationOptions . OnlyOnFaulted | TaskContinuationOptions . ExecuteSynchronously ;
42
- private static void continuation ( Task t , object s ) => t . Exception . Handle ( e => true ) ;
42
+ private static void IgnoreTaskContinuation ( Task t , object s ) => t . Exception . Handle ( e => true ) ;
43
43
#endif
44
44
45
45
public static Task TimeoutAfter ( this Task task , TimeSpan timeout )
@@ -68,12 +68,7 @@ static async Task DoTimeoutAfter(Task task, TimeSpan timeout)
68
68
Task resultTask = await Task . WhenAny ( task , delayTask ) . ConfigureAwait ( false ) ;
69
69
if ( resultTask == delayTask )
70
70
{
71
- Task supressErrorTask = task . ContinueWith (
72
- continuationAction : continuation ,
73
- state : null ,
74
- cancellationToken : CancellationToken . None ,
75
- continuationOptions : s_tco ,
76
- scheduler : TaskScheduler . Default ) ;
71
+ task . Ignore ( ) ;
77
72
throw new TimeoutException ( ) ;
78
73
}
79
74
else
@@ -112,12 +107,7 @@ static async ValueTask DoTimeoutAfter(ValueTask valueTask, TimeSpan timeout)
112
107
Task resultTask = await Task . WhenAny ( task , delayTask ) . ConfigureAwait ( false ) ;
113
108
if ( resultTask == delayTask )
114
109
{
115
- Task supressErrorTask = task . ContinueWith (
116
- continuationAction : continuation ,
117
- state : null ,
118
- cancellationToken : CancellationToken . None ,
119
- continuationOptions : s_tco ,
120
- scheduler : TaskScheduler . Default ) ;
110
+ task . Ignore ( ) ;
121
111
throw new TimeoutException ( ) ;
122
112
}
123
113
else
@@ -155,5 +145,26 @@ public static void EnsureCompleted(this ValueTask task)
155
145
{
156
146
task . GetAwaiter ( ) . GetResult ( ) ;
157
147
}
148
+
149
+ #if ! NET6_0_OR_GREATER
150
+ // https://github.com/dotnet/runtime/issues/23878
151
+ // https://github.com/dotnet/runtime/issues/23878#issuecomment-1398958645
152
+ public static void Ignore ( this Task task )
153
+ {
154
+ if ( task . IsCompleted )
155
+ {
156
+ _ = task . Exception ;
157
+ }
158
+ else
159
+ {
160
+ _ = task . ContinueWith (
161
+ continuationAction : IgnoreTaskContinuation ,
162
+ state : null ,
163
+ cancellationToken : CancellationToken . None ,
164
+ continuationOptions : s_tco ,
165
+ scheduler : TaskScheduler . Default ) ;
166
+ }
167
+ }
168
+ #endif
158
169
}
159
170
}
0 commit comments