@@ -43,25 +43,34 @@ internal static class TaskExtensions
43
43
44
44
private static void continuation ( Task t , object s ) => t . Exception . Handle ( e => true ) ;
45
45
46
- public static async Task TimeoutAfter ( this Task task , TimeSpan timeout )
46
+ public static Task TimeoutAfter ( this Task task , TimeSpan timeout )
47
47
{
48
- #if NET6_0_OR_GREATER
49
- await task . WaitAsync ( timeout )
50
- . ConfigureAwait ( false ) ;
51
- #else
52
- if ( task == await Task . WhenAny ( task , Task . Delay ( timeout ) ) . ConfigureAwait ( false ) )
48
+ if ( task . IsCompletedSuccessfully )
53
49
{
54
- await task . ConfigureAwait ( false ) ;
50
+ return task ;
55
51
}
56
- else
52
+
53
+ #if NET6_0_OR_GREATER
54
+ return task . WaitAsync ( timeout ) ;
55
+ #else
56
+ return TimeoutAfterImplement ( task , timeout ) ;
57
+
58
+ static async Task TimeoutAfterImplement ( Task task , TimeSpan timeout )
57
59
{
58
- Task supressErrorTask = task . ContinueWith (
59
- continuationAction : continuation ,
60
- state : null ,
61
- cancellationToken : CancellationToken . None ,
62
- continuationOptions : s_tco ,
63
- scheduler : TaskScheduler . Default ) ;
64
- throw new TimeoutException ( ) ;
60
+ if ( task == await Task . WhenAny ( task , Task . Delay ( timeout ) ) . ConfigureAwait ( false ) )
61
+ {
62
+ await task . ConfigureAwait ( false ) ;
63
+ }
64
+ else
65
+ {
66
+ Task supressErrorTask = task . ContinueWith (
67
+ continuationAction : continuation ,
68
+ state : null ,
69
+ cancellationToken : CancellationToken . None ,
70
+ continuationOptions : s_tco ,
71
+ scheduler : TaskScheduler . Default ) ;
72
+ throw new TimeoutException ( ) ;
73
+ }
65
74
}
66
75
#endif
67
76
}
0 commit comments