@@ -107,3 +107,81 @@ public static async Task<TResult> WaitAsync<TResult>(
107107}
108108
109109#endif
110+ #if ! NET8_0_OR_GREATER
111+
112+ /// <summary>Configures an awaiter used to await this <see cref="Task"/>.</summary>
113+ //Link: https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.task.configureawait?view=net-11.0#system-threading-tasks-task-configureawait(system-threading-tasks-configureawaitoptions)
114+ public static ConfiguredTaskAwaitable ConfigureAwait ( this Task target , ConfigureAwaitOptions options )
115+ {
116+ if ( ( options & ~ ( ConfigureAwaitOptions . ContinueOnCapturedContext |
117+ ConfigureAwaitOptions . SuppressThrowing |
118+ ConfigureAwaitOptions . ForceYielding ) ) != 0 )
119+ {
120+ throw new ArgumentOutOfRangeException ( nameof ( options ) ) ;
121+ }
122+
123+ var task = target ;
124+
125+ if ( ( options & ConfigureAwaitOptions . ForceYielding ) != 0 )
126+ {
127+ task = ForceYieldAsync ( task ) ;
128+
129+ static async Task ForceYieldAsync ( Task t )
130+ {
131+ await Task . Yield ( ) ;
132+ await t ;
133+ }
134+ }
135+
136+ if ( ( options & ConfigureAwaitOptions . SuppressThrowing ) != 0 )
137+ {
138+ task = SuppressThrowAsync ( task ) ;
139+
140+ static async Task SuppressThrowAsync ( Task t )
141+ {
142+ try
143+ {
144+ await t ;
145+ }
146+ catch
147+ {
148+ }
149+ }
150+ }
151+
152+ return task . ConfigureAwait (
153+ ( options & ConfigureAwaitOptions . ContinueOnCapturedContext ) != 0 ) ;
154+ }
155+
156+ /// <summary>Configures an awaiter used to await this <see cref="Task{TResult}"/>.</summary>
157+ //Link: https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.task-1.configureawait?view=net-11.0#system-threading-tasks-task-1-configureawait(system-threading-tasks-configureawaitoptions)
158+ public static ConfiguredTaskAwaitable < TResult > ConfigureAwait < TResult > (
159+ this Task < TResult > target ,
160+ ConfigureAwaitOptions options )
161+ {
162+ if ( ( options & ~ ( ConfigureAwaitOptions . ContinueOnCapturedContext |
163+ ConfigureAwaitOptions . ForceYielding ) ) != 0 )
164+ {
165+ throw new ArgumentOutOfRangeException ( nameof ( options ) ) ;
166+ }
167+
168+ var task = target ;
169+
170+ if ( ( options & ConfigureAwaitOptions . ForceYielding ) != 0 )
171+ {
172+ task = ForceYieldAsync ( task ) ;
173+
174+ static async Task < TResult > ForceYieldAsync ( Task < TResult > t )
175+ {
176+ await Task . Yield ( ) ;
177+ return await t ;
178+ }
179+ }
180+
181+ return task . ConfigureAwait (
182+ ( options & ConfigureAwaitOptions . ContinueOnCapturedContext ) != 0 ) ;
183+ }
184+
185+ #endif
186+ }
187+
0 commit comments