File tree Expand file tree Collapse file tree 1 file changed +14
-13
lines changed Expand file tree Collapse file tree 1 file changed +14
-13
lines changed Original file line number Diff line number Diff line change @@ -51,22 +51,23 @@ public static async Task ForEachAsync<TSource>(
51
51
if ( body == null ) throw new ArgumentNullException ( nameof ( body ) ) ;
52
52
if ( degreeOfParallelism < 1 ) throw new ArgumentOutOfRangeException ( nameof ( degreeOfParallelism ) , "Degree of parallelism must be at least 1." ) ;
53
53
54
- var semaphore = new SemaphoreSlim ( degreeOfParallelism ) ;
55
-
56
- var tasks = source . Select ( async item =>
54
+ using ( var semaphore = new SemaphoreSlim ( degreeOfParallelism ) )
57
55
{
58
- await semaphore . WaitAsync ( ) ;
59
- try
60
- {
61
- await body ( item ) ;
62
- }
63
- finally
56
+ var tasks = source . Select ( async item =>
64
57
{
65
- semaphore . Release ( ) ;
66
- }
67
- } ) ;
58
+ await semaphore . WaitAsync ( ) . ConfigureAwait ( false ) ;
59
+ try
60
+ {
61
+ await body ( item ) . ConfigureAwait ( false ) ;
62
+ }
63
+ finally
64
+ {
65
+ semaphore . Release ( ) ;
66
+ }
67
+ } ) ;
68
68
69
- await Task . WhenAll ( tasks ) ;
69
+ await Task . WhenAll ( tasks ) . ConfigureAwait ( false ) ;
70
+ }
70
71
}
71
72
}
72
73
}
You can’t perform that action at this time.
0 commit comments