1+ using Newtonsoft . Json ;
12using System ;
23using System . IO ;
34using System . Net . Http ;
45using System . Text ;
56using System . Threading ;
67using System . Threading . Tasks ;
7- using Newtonsoft . Json ;
88
99namespace Docker . DotNet . Models
1010{
@@ -20,7 +20,7 @@ internal static async Task MonitorStreamAsync(Task<Stream> streamTask, DockerCli
2020 using ( var reader = new StreamReader ( stream , new UTF8Encoding ( false ) ) )
2121 {
2222 string line ;
23- while ( ( line = await reader . ReadLineAsync ( ) ) != null && ! cancel . IsCancellationRequested )
23+ while ( ! cancel . IsCancellationRequested && ( line = await reader . ReadLineAsync ( ) ) != null )
2424 {
2525 progress . Report ( line ) ;
2626 }
@@ -34,7 +34,7 @@ internal static async Task MonitorStreamForMessagesAsync<T>(Task<Stream> streamT
3434 using ( var reader = new StreamReader ( stream , new UTF8Encoding ( false ) ) )
3535 using ( var jsonReader = new JsonTextReader ( reader ) { SupportMultipleContent = true } )
3636 {
37- while ( await jsonReader . ReadAsync ( ) . WithCancellation ( cancel ) )
37+ while ( ! cancel . IsCancellationRequested && await jsonReader . ReadAsync ( ) )
3838 {
3939 var ev = _serializer . Deserialize < T > ( jsonReader ) ;
4040 progress ? . Report ( ev ) ;
@@ -56,7 +56,7 @@ internal static async Task MonitorResponseForMessagesAsync<T>(Task<HttpResponseM
5656 string line ;
5757 try
5858 {
59- while ( ( line = await reader . ReadLineAsync ( ) ) != null && ! cancel . IsCancellationRequested )
59+ while ( ! cancel . IsCancellationRequested && ( line = await reader . ReadLineAsync ( ) ) != null )
6060 {
6161 var prog = client . JsonSerializer . DeserializeObject < T > ( line ) ;
6262 if ( prog == null ) continue ;
@@ -73,19 +73,5 @@ internal static async Task MonitorResponseForMessagesAsync<T>(Task<HttpResponseM
7373 }
7474 }
7575 }
76-
77- private static async Task < T > WithCancellation < T > ( this Task < T > task , CancellationToken cancellationToken )
78- {
79- var tcs = new TaskCompletionSource < bool > ( ) ;
80- using ( cancellationToken . Register ( s => ( ( TaskCompletionSource < bool > ) s ) . TrySetResult ( true ) , tcs ) )
81- {
82- if ( task != await Task . WhenAny ( task , tcs . Task ) )
83- {
84- throw new OperationCanceledException ( cancellationToken ) ;
85- }
86- }
87-
88- return await task ;
89- }
9076 }
9177}
0 commit comments