From d6b9b7355cced7a39aa9e9d0a4f62f62b0d84e2d Mon Sep 17 00:00:00 2001 From: Weihan Li Date: Thu, 30 Nov 2023 23:01:45 +0800 Subject: [PATCH] sample: add DisposeTestSample --- samples/DotNetCoreSample/DisposeTest.cs | 104 ++++++++++++++++++++++++ samples/DotNetCoreSample/Program.cs | 19 ++++- 2 files changed, 120 insertions(+), 3 deletions(-) create mode 100644 samples/DotNetCoreSample/DisposeTest.cs diff --git a/samples/DotNetCoreSample/DisposeTest.cs b/samples/DotNetCoreSample/DisposeTest.cs new file mode 100644 index 00000000..0340a652 --- /dev/null +++ b/samples/DotNetCoreSample/DisposeTest.cs @@ -0,0 +1,104 @@ +// Copyright (c) Weihan Li. All rights reserved. +// Licensed under the Apache license. + +using WeihanLi.Common.Helpers; + +namespace DotNetCoreSample; + +public class DisposeTest +{ + public static void MainTest() + { + Console.WriteLine(@$"---- {nameof(MainTest)} start"); + { + using var service = new TestService() + { + Name = "MainTest" + }; + } + { + var service = new TestService() + { + Name = "MainTest1" + }; + service.Dispose(); + } + { + // forget to dispose + var service = new TestService() + { + Name = "MainTest2" + }; + Console.WriteLine(service.GetType()); + + service = null; + Console.WriteLine(service is null); + } + GC.Collect(); + GC.WaitForPendingFinalizers(); + + Console.WriteLine(@$"---- {nameof(MainTest)} end"); + } + + public static async ValueTask MainTestAsync() + { + Console.WriteLine(@$"---- {nameof(MainTestAsync)} start"); + { + await using var service = new TestService() + { + Name = "MainTestAsync" + }; + } + { + var service = new TestService() + { + Name = "MainTestAsync1" + }; + await service.DisposeAsync(); + } + { + // forget to dispose + var service = new TestService() + { + Name = "MainTestAsync2" + }; + Console.WriteLine(service.GetType()); + + service = null; + Console.WriteLine(service is null); + } + GC.Collect(); + GC.WaitForPendingFinalizers(); + + Console.WriteLine(@$"---- {nameof(MainTestAsync)} end"); + } +} + +file sealed class TestService : DisposableBase +{ + public required string Name { get; init; } + + protected override void Dispose(bool disposing) + { + if (disposing) + { + Console.WriteLine($@"<<{Name}>> disposes managed resources"); + } + Console.WriteLine($@"<<{Name}>> disposes unmanaged resources"); + base.Dispose(disposing); + } + + protected override async ValueTask DisposeAsyncCore() + { + { + Console.WriteLine($@"<<{Name}>> disposes managed resources async"); + await Task.Yield(); + } + await base.DisposeAsyncCore(); + } + + ~TestService() + { + Console.WriteLine($@"<<{Name}>> Finalizer executing"); + } +} diff --git a/samples/DotNetCoreSample/Program.cs b/samples/DotNetCoreSample/Program.cs index ad98dded..006cff1c 100644 --- a/samples/DotNetCoreSample/Program.cs +++ b/samples/DotNetCoreSample/Program.cs @@ -320,11 +320,24 @@ // Console.WriteLine(@"Exited"); // }); // ApplicationHelper.RuntimeInfo.Dump(); -Console.WriteLine(ApplicationHelper.ResolvePath("yarn.cmd")); -await AppHostTest.MainTest(); +// Console.WriteLine(ApplicationHelper.ResolvePath("yarn.cmd")); +// await AppHostTest.MainTest(); // NewtonJsonFormatterTest.MainTest(); -// ConsoleHelper.ReadKeyWithPrompt("Press any key to exit"); +DisposeTest.MainTest(); +Console.WriteLine(); +await DisposeTest.MainTestAsync(); +Console.WriteLine(); + +ConsoleHelper.ReadKeyWithPrompt("Press any key to continue"); + +await DisposeTest.MainTestAsync(); +Console.WriteLine(); + +GC.Collect(); +GC.WaitForPendingFinalizers(); + +ConsoleHelper.ReadKeyWithPrompt("Press any key to exit"); internal struct TestStruct {