Skip to content

Commit ef16830

Browse files
committed
Target core 3.0 and adapt to IAsyncDisposable
1 parent 7c12f6c commit ef16830

5 files changed

Lines changed: 55 additions & 42 deletions

File tree

FollowingFileStream.ConsoleTestTool/FollowingFileStream.ConsoleTestTool.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<PropertyGroup>
88
<OutputType>Exe</OutputType>
9-
<TargetFramework>netcoreapp2.2</TargetFramework>
9+
<TargetFramework>netcoreapp3.0</TargetFramework>
1010
<LangVersion>7.1</LangVersion>
1111
<IsPackable>false</IsPackable>
1212
</PropertyGroup>

FollowingFileStream.Tests/FollowingFileStream.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFramework>netcoreapp2.2</TargetFramework>
3+
<TargetFramework>netcoreapp3.0</TargetFramework>
44
<IsPackable>false</IsPackable>
55
</PropertyGroup>
66
<ItemGroup>

FollowingFileStream/AsyncStream.cs

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -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

FollowingFileStream/FollowingFileStream.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -302,28 +302,33 @@ private bool IsFileLockedForWriting()
302302
return false;
303303
}
304304

305-
bool disposed = false;
305+
private bool disposed = false;
306306

307307
/// <summary>
308308
/// Releases the unmanaged resources used by the FollowingFileStream and optionally
309309
/// releases the managed resources.
310310
/// </summary>
311311
/// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.
312312
///</param>
313-
protected override void Dispose(bool disposing)
313+
protected override async ValueTask DisposeAsync(bool disposing)
314314
{
315315
if (disposed)
316316
return;
317317

318-
if (disposing)
318+
try
319319
{
320-
cts.Cancel();
321-
fileStream.Dispose();
320+
if (disposing)
321+
{
322+
cts.Cancel();
323+
await fileStream.DisposeAsync();
324+
}
325+
}
326+
finally
327+
{
328+
disposed = true;
329+
// Call stream class implementation.
330+
base.Dispose(disposing);
322331
}
323-
324-
disposed = true;
325-
// Call stream class implementation.
326-
base.Dispose(disposing);
327332
}
328333

329334
/// <summary>

FollowingFileStream/FollowingFileStream.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<OutputType>Library</OutputType>
4-
<TargetFramework>netcoreapp2.2</TargetFramework>
4+
<TargetFramework>netcoreapp3.0</TargetFramework>
55
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
66
</PropertyGroup>
77
<PropertyGroup>

0 commit comments

Comments
 (0)