From 144f3e5ca66b3fce73dd1da7a853116c850e71dc Mon Sep 17 00:00:00 2001 From: root <295172551@qq.com> Date: Sun, 15 Jun 2025 12:41:42 +0800 Subject: [PATCH] =?UTF-8?q?CellularNetworkContext=20=20=20=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E4=B8=80=E4=B8=8B=20Dispose?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Contexts/CellularNetworkContext.cs | 43 ++++++++++++++++++- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/CoreAgent.Infrastructure/Contexts/CellularNetworkContext.cs b/CoreAgent.Infrastructure/Contexts/CellularNetworkContext.cs index ef8e3c6..ac5f0a2 100644 --- a/CoreAgent.Infrastructure/Contexts/CellularNetworkContext.cs +++ b/CoreAgent.Infrastructure/Contexts/CellularNetworkContext.cs @@ -264,6 +264,16 @@ public class CellularNetworkContext : ICellularNetworkContext, IDisposable /// 释放资源 /// public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + /// + /// 释放资源 + /// + /// 是否正在释放托管资源 + protected virtual void Dispose(bool disposing) { if (_isDisposed) { @@ -277,10 +287,39 @@ public class CellularNetworkContext : ICellularNetworkContext, IDisposable return; } - _token?.Cancel(); - _token?.Dispose(); + if (disposing) + { + try + { + if (_token != null) + { + if (!_token.IsCancellationRequested) + { + _token.Cancel(); + } + _token.Dispose(); + } + } + catch (ObjectDisposedException) + { + // 忽略已释放的异常 + } + catch (Exception) + { + // 记录其他异常 + } + } + _isInitialized = false; _isDisposed = true; } } + + /// + /// 析构函数 + /// + ~CellularNetworkContext() + { + Dispose(false); + } } \ No newline at end of file