Browse Source

CellularNetworkContext 优化一下 Dispose

master
root 2 days ago
parent
commit
144f3e5ca6
  1. 43
      CoreAgent.Infrastructure/Contexts/CellularNetworkContext.cs

43
CoreAgent.Infrastructure/Contexts/CellularNetworkContext.cs

@ -264,6 +264,16 @@ public class CellularNetworkContext : ICellularNetworkContext, IDisposable
/// 释放资源
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// 释放资源
/// </summary>
/// <param name="disposing">是否正在释放托管资源</param>
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;
}
}
/// <summary>
/// 析构函数
/// </summary>
~CellularNetworkContext()
{
Dispose(false);
}
}
Loading…
Cancel
Save