添加暂停检测时不进行重连操作和连接检测的功能
This commit is contained in:
@@ -204,12 +204,17 @@ namespace JoyD.Windows.CS.Toprie
|
|||||||
// 设置暂停状态
|
// 设置暂停状态
|
||||||
pauseDetectionToolStripMenuItem.Text = "恢复检测";
|
pauseDetectionToolStripMenuItem.Text = "恢复检测";
|
||||||
|
|
||||||
// 暂停时停止图像接收
|
// 暂停时停止图像接收并更新DeviceManager的暂停检测状态
|
||||||
if (_isReceivingImage && _deviceManager != null)
|
if (_deviceManager != null)
|
||||||
|
{
|
||||||
|
_deviceManager.IsDetectionPaused = true;
|
||||||
|
|
||||||
|
if (_isReceivingImage)
|
||||||
{
|
{
|
||||||
_deviceManager.StopImageReceiving();
|
_deviceManager.StopImageReceiving();
|
||||||
_isReceivingImage = false;
|
_isReceivingImage = false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Console.WriteLine("检测已暂停");
|
Console.WriteLine("检测已暂停");
|
||||||
}
|
}
|
||||||
@@ -218,12 +223,25 @@ namespace JoyD.Windows.CS.Toprie
|
|||||||
// 设置恢复状态
|
// 设置恢复状态
|
||||||
pauseDetectionToolStripMenuItem.Text = "暂停检测";
|
pauseDetectionToolStripMenuItem.Text = "暂停检测";
|
||||||
|
|
||||||
// 恢复时,立即停止并重新开始图像接收,确保获取最新图像
|
// 恢复时更新DeviceManager的暂停检测状态并重新开始图像接收
|
||||||
if (_deviceManager != null && _deviceManager.ConnectionStatus == ConnectionStatus.Connected)
|
if (_deviceManager != null)
|
||||||
|
{
|
||||||
|
_deviceManager.IsDetectionPaused = false;
|
||||||
|
|
||||||
|
if (_deviceManager.ConnectionStatus == ConnectionStatus.Connected)
|
||||||
{
|
{
|
||||||
_deviceManager.StopImageReceiving();
|
_deviceManager.StopImageReceiving();
|
||||||
_deviceManager.StartImageReceiving();
|
_deviceManager.StartImageReceiving();
|
||||||
_isReceivingImage = true;
|
_isReceivingImage = true;
|
||||||
|
|
||||||
|
// 恢复检测后,启动连接检查以确保连接正常
|
||||||
|
_deviceManager.StartConnectionCheck();
|
||||||
|
}
|
||||||
|
// 如果当前是断开状态但启用了自动重连,尝试启动重连
|
||||||
|
else if (_deviceManager.AutoReconnectEnabled)
|
||||||
|
{
|
||||||
|
_deviceManager.StartAutoReconnect();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Console.WriteLine("检测已恢复");
|
Console.WriteLine("检测已恢复");
|
||||||
|
|||||||
@@ -153,6 +153,9 @@ namespace JoyD.Windows.CS.Toprie
|
|||||||
// 设计模式标志,用于在设计模式下跳过实际的设备连接和初始化
|
// 设计模式标志,用于在设计模式下跳过实际的设备连接和初始化
|
||||||
public static bool IsDesignMode { get; set; } = true;
|
public static bool IsDesignMode { get; set; } = true;
|
||||||
|
|
||||||
|
// 暂停检测标志,用于控制是否进行连接检测和重连操作
|
||||||
|
public bool IsDetectionPaused { get; set; } = false;
|
||||||
|
|
||||||
// 项目路径,用于数据文件的存取
|
// 项目路径,用于数据文件的存取
|
||||||
private string _projectPath = "";
|
private string _projectPath = "";
|
||||||
|
|
||||||
@@ -185,7 +188,6 @@ namespace JoyD.Windows.CS.Toprie
|
|||||||
// 当前视频模式
|
// 当前视频模式
|
||||||
private VideoMode _currentVideoMode = VideoMode.Infrared; // 默认红外模式
|
private VideoMode _currentVideoMode = VideoMode.Infrared; // 默认红外模式
|
||||||
// 自动重连是否启用
|
// 自动重连是否启用
|
||||||
private readonly bool _autoReconnectEnabled = true;
|
|
||||||
// 自动重连定时器
|
// 自动重连定时器
|
||||||
private System.Threading.Timer _reconnectTimer;
|
private System.Threading.Timer _reconnectTimer;
|
||||||
// 重连间隔(毫秒)
|
// 重连间隔(毫秒)
|
||||||
@@ -378,7 +380,7 @@ namespace JoyD.Windows.CS.Toprie
|
|||||||
|
|
||||||
// 保存状态变更相关信息供后续处理
|
// 保存状态变更相关信息供后续处理
|
||||||
ConnectionStatus finalNewStatus = newStatus;
|
ConnectionStatus finalNewStatus = newStatus;
|
||||||
bool shouldReconnect = (newStatus == ConnectionStatus.Disconnected && _autoReconnectEnabled && oldStatus != ConnectionStatus.Connecting);
|
bool shouldReconnect = (newStatus == ConnectionStatus.Disconnected && _isAutoReconnectEnabled && oldStatus != ConnectionStatus.Connecting);
|
||||||
bool shouldReset = (newStatus == ConnectionStatus.Connected);
|
bool shouldReset = (newStatus == ConnectionStatus.Connected);
|
||||||
|
|
||||||
// 添加状态转换验证,避免不合理的状态切换
|
// 添加状态转换验证,避免不合理的状态切换
|
||||||
@@ -866,7 +868,7 @@ namespace JoyD.Windows.CS.Toprie
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 启动连接状态检查
|
/// 启动连接状态检查
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void StartConnectionCheck()
|
public void StartConnectionCheck()
|
||||||
{
|
{
|
||||||
lock (_lockObject) // 添加线程同步锁
|
lock (_lockObject) // 添加线程同步锁
|
||||||
{
|
{
|
||||||
@@ -879,6 +881,13 @@ namespace JoyD.Windows.CS.Toprie
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 在暂停检测模式下,跳过连接检查
|
||||||
|
if (IsDetectionPaused)
|
||||||
|
{
|
||||||
|
Log("暂停检测模式下跳过连接检查");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// 首先停止现有的连接检查,确保资源释放
|
// 首先停止现有的连接检查,确保资源释放
|
||||||
StopConnectionCheck();
|
StopConnectionCheck();
|
||||||
|
|
||||||
@@ -925,7 +934,7 @@ namespace JoyD.Windows.CS.Toprie
|
|||||||
Log("重新初始化失败,确认连接已断开");
|
Log("重新初始化失败,确认连接已断开");
|
||||||
UpdateConnectionStatus(ConnectionStatus.Disconnected, "设备未初始化,连接已断开");
|
UpdateConnectionStatus(ConnectionStatus.Disconnected, "设备未初始化,连接已断开");
|
||||||
// 启动自动重连
|
// 启动自动重连
|
||||||
if (_autoReconnectEnabled)
|
if (_isAutoReconnectEnabled)
|
||||||
{
|
{
|
||||||
StartAutoReconnect();
|
StartAutoReconnect();
|
||||||
}
|
}
|
||||||
@@ -974,8 +983,8 @@ namespace JoyD.Windows.CS.Toprie
|
|||||||
{
|
{
|
||||||
UpdateConnectionStatus(ConnectionStatus.Disconnected, "连接检查异常", ex);
|
UpdateConnectionStatus(ConnectionStatus.Disconnected, "连接检查异常", ex);
|
||||||
_isInitialized = false;
|
_isInitialized = false;
|
||||||
// 启动自动重连
|
// 启动自动重连,只有在未暂停检测时才执行
|
||||||
if (_autoReconnectEnabled)
|
if (_isAutoReconnectEnabled && !IsDetectionPaused)
|
||||||
{
|
{
|
||||||
StartAutoReconnect();
|
StartAutoReconnect();
|
||||||
}
|
}
|
||||||
@@ -1027,9 +1036,11 @@ namespace JoyD.Windows.CS.Toprie
|
|||||||
// 先检查对象状态,避免不必要的操作
|
// 先检查对象状态,避免不必要的操作
|
||||||
lock (_lockObject)
|
lock (_lockObject)
|
||||||
{
|
{
|
||||||
if (_isDisposed || _connectionStatus != ConnectionStatus.Connected)
|
if (_isDisposed || _connectionStatus != ConnectionStatus.Connected || IsDetectionPaused)
|
||||||
{
|
{
|
||||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionWrapper() - 对象已释放或连接状态非Connected,跳过检查");
|
string reason = _isDisposed ? "对象已释放" :
|
||||||
|
(_connectionStatus != ConnectionStatus.Connected ? "连接状态非Connected" : "检测已暂停");
|
||||||
|
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionWrapper() - {reason},跳过检查");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1086,8 +1097,8 @@ namespace JoyD.Windows.CS.Toprie
|
|||||||
|
|
||||||
UpdateConnectionStatus(ConnectionStatus.Disconnected, "连接已断开:设备离线");
|
UpdateConnectionStatus(ConnectionStatus.Disconnected, "连接已断开:设备离线");
|
||||||
|
|
||||||
// 断开连接后自动启动重连,但在锁外执行
|
// 断开连接后自动启动重连,但在锁外执行,只有在未暂停检测时才执行
|
||||||
if (_autoReconnectEnabled)
|
if (_isAutoReconnectEnabled && !IsDetectionPaused)
|
||||||
{
|
{
|
||||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionWrapper() - 连接断开,将启动自动重连");
|
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] CheckConnectionWrapper() - 连接断开,将启动自动重连");
|
||||||
// 在锁外启动自动重连,避免潜在的死锁
|
// 在锁外启动自动重连,避免潜在的死锁
|
||||||
@@ -1115,6 +1126,13 @@ namespace JoyD.Windows.CS.Toprie
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 在暂停检测模式下,跳过连接有效性检查,直接返回连接有效
|
||||||
|
if (IsDetectionPaused)
|
||||||
|
{
|
||||||
|
Log("暂停检测模式下跳过连接有效性检查,模拟连接有效");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
// 注意:此方法被CheckConnectionWrapper调用,已经在线程安全的上下文中
|
// 注意:此方法被CheckConnectionWrapper调用,已经在线程安全的上下文中
|
||||||
// 不需要额外加锁,但需要确保SDK实例的访问是线程安全的
|
// 不需要额外加锁,但需要确保SDK实例的访问是线程安全的
|
||||||
|
|
||||||
@@ -3375,7 +3393,7 @@ namespace JoyD.Windows.CS.Toprie
|
|||||||
// 连接进行中标志(用于防止重连期间再次触发连接)
|
// 连接进行中标志(用于防止重连期间再次触发连接)
|
||||||
private volatile bool _isConnecting = false;
|
private volatile bool _isConnecting = false;
|
||||||
|
|
||||||
private void StartAutoReconnect()
|
public void StartAutoReconnect()
|
||||||
{
|
{
|
||||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] StartAutoReconnect() - 开始执行");
|
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] StartAutoReconnect() - 开始执行");
|
||||||
|
|
||||||
@@ -3386,6 +3404,13 @@ namespace JoyD.Windows.CS.Toprie
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 在暂停检测模式下,跳过重连启动
|
||||||
|
if (IsDetectionPaused)
|
||||||
|
{
|
||||||
|
Log("暂停检测模式下跳过重连启动");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// 检查对象是否已释放
|
// 检查对象是否已释放
|
||||||
if (_isDisposed)
|
if (_isDisposed)
|
||||||
{
|
{
|
||||||
@@ -3591,6 +3616,13 @@ namespace JoyD.Windows.CS.Toprie
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 在暂停检测模式下,跳过重连操作
|
||||||
|
if (IsDetectionPaused)
|
||||||
|
{
|
||||||
|
Log("暂停检测模式下跳过实际的重连操作");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] ReconnectCallback() - 开始执行");
|
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] ReconnectCallback() - 开始执行");
|
||||||
|
|
||||||
// 使用Interlocked.Exchange实现原子操作检查,防止重入
|
// 使用Interlocked.Exchange实现原子操作检查,防止重入
|
||||||
|
|||||||
Reference in New Issue
Block a user