@@ -284,23 +284,26 @@ public sealed override void Write(byte[] buffer, int offset, int count)
284284 /// </exception>
285285 public abstract override Task WriteAsync ( byte [ ] buffer , int offset , int count , CancellationToken cancellationToken ) ;
286286
287- private bool disposed = false ;
287+ /// <summary>
288+ /// Asynchronously releases the unmanaged resources used by the FollowingFileStream and optionally
289+ /// releases the managed resources.
290+ /// </summary>
291+ /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.
292+ ///</param>
293+ protected virtual ValueTask DisposeAsync ( bool disposing ) => default ;
294+
295+ /// <summary>
296+ /// Asynchronously releases all resources used by the AsyncStream.
297+ /// </summary>
298+ public sealed override ValueTask DisposeAsync ( ) => DisposeAsync ( true ) ;
288299
289300 /// <summary>
290301 /// Releases the unmanaged resources used by the FollowingFileStream and optionally
291302 /// releases the managed resources.
292303 /// </summary>
293304 /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.
294305 ///</param>
295- protected override void Dispose ( bool disposing )
296- {
297- if ( disposed )
298- return ;
299-
300- disposed = true ;
301- // Call stream class implementation.
302- base . Dispose ( disposing ) ;
303- }
306+ protected sealed override void Dispose ( bool disposing ) => DisposeAsync ( disposing ) . GetAwaiter ( ) . GetResult ( ) ;
304307
305308 /// <summary>
306309 /// Synchronized version of an async stream
@@ -398,26 +401,6 @@ public override int WriteTimeout
398401 }
399402 }
400403
401- protected override void Dispose ( bool disposing )
402- {
403- try
404- {
405- // Explicitly pick up a potentially methodimpl'ed Dispose
406- if ( disposing )
407- {
408- cts . Cancel ( ) ;
409- using ( locker . Lock ( ) )
410- {
411- ( ( IDisposable ) _stream ) . Dispose ( ) ;
412- }
413- }
414- }
415- finally
416- {
417- base . Dispose ( disposing ) ;
418- }
419- }
420-
421404 public override long Seek ( long offset , SeekOrigin origin )
422405 {
423406 using ( locker . Lock ( cts . Token ) )
@@ -479,6 +462,31 @@ public override async Task FlushAsync(CancellationToken cancellationToken)
479462 cancellationToken . ThrowIfCancellationRequested ( ) ;
480463 }
481464 }
465+
466+ private bool disposed = false ;
467+
468+ protected override async ValueTask DisposeAsync ( bool disposing )
469+ {
470+ if ( disposed )
471+ return ;
472+
473+ try
474+ {
475+ // Explicitly pick up a potentially methodimpl'ed DisposeAsync
476+ if ( disposing )
477+ {
478+ cts . Cancel ( ) ;
479+ using ( await locker . LockAsync ( ) )
480+ {
481+ await ( ( IAsyncDisposable ) _stream ) . DisposeAsync ( ) ;
482+ }
483+ }
484+ }
485+ finally
486+ {
487+ await base . DisposeAsync ( disposing ) ;
488+ }
489+ }
482490 }
483491 }
484492
0 commit comments