From 44fd7d08f41569f39478849b112560e8f38443e8 Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Sun, 20 Oct 2024 19:38:04 -0700 Subject: [PATCH] Add UnsafeDispose methods to ComServer and WinRtServer --- Directory.Build.props | 2 +- src/Shmuelie.WinRTServer/ComServer.cs | 29 +++++++++++++++++++ .../Shmuelie.WinRTServer.csproj | 2 +- src/Shmuelie.WinRTServer/WinRtServer.cs | 25 ++++++++++++++++ 4 files changed, 56 insertions(+), 2 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index 7957efe..40cf470 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -19,7 +19,7 @@ As such, this needs to be changed before a new release as well. --> - 2.0.0 + 2.1.0 false diff --git a/src/Shmuelie.WinRTServer/ComServer.cs b/src/Shmuelie.WinRTServer/ComServer.cs index 86ed017..471f0c4 100644 --- a/src/Shmuelie.WinRTServer/ComServer.cs +++ b/src/Shmuelie.WinRTServer/ComServer.cs @@ -257,6 +257,35 @@ public bool IsDisposed private set; } + /// + /// Force the server to stop and release all resources. + /// + /// Unlike , will ignore if any objects are still alive before unregistering class factories. + /// + public void UnsafeDispose() + { + if (!IsDisposed) + { + try + { + _ = CoSuspendClassObjects(); + + liveServers.Clear(); + lifetimeCheckTimer.Stop(); + lifetimeCheckTimer.Dispose(); + + foreach (var clsid in factories.Keys) + { + _ = UnregisterClassFactory(clsid); + } + } + finally + { + IsDisposed = true; + } + } + } + /// public async ValueTask DisposeAsync() { diff --git a/src/Shmuelie.WinRTServer/Shmuelie.WinRTServer.csproj b/src/Shmuelie.WinRTServer/Shmuelie.WinRTServer.csproj index e7a5e50..0b36afe 100644 --- a/src/Shmuelie.WinRTServer/Shmuelie.WinRTServer.csproj +++ b/src/Shmuelie.WinRTServer/Shmuelie.WinRTServer.csproj @@ -2,7 +2,7 @@ net8.0-windows10.0.22000.0 true - Remove support for .NET Standard 2.0 and made package AOT compatible. + Add UnsafeDispose methods to ComServer and WinRtServer. True True win-x64;win-x86;win-arm64;$(RuntimeIdentifiers) diff --git a/src/Shmuelie.WinRTServer/WinRtServer.cs b/src/Shmuelie.WinRTServer/WinRtServer.cs index f263232..62b3e12 100644 --- a/src/Shmuelie.WinRTServer/WinRtServer.cs +++ b/src/Shmuelie.WinRTServer/WinRtServer.cs @@ -310,6 +310,31 @@ public void Stop() return await local.Task.ConfigureAwait(false); } + /// + /// Force the server to stop and release all resources. + /// + /// Unlike , will ignore if any objects are still alive before unregistering activation factories. + /// + public void UnsafeDispose() + { + if (!IsDisposed) + { + try + { + liveServers.Clear(); + lifetimeCheckTimer.Stop(); + lifetimeCheckTimer.Dispose(); + + RoRevokeActivationFactories(registrationCookie); + registrationCookie = (RO_REGISTRATION_COOKIE)0; + } + finally + { + IsDisposed = true; + } + } + } + /// public async ValueTask DisposeAsync() {