修复设计模式下控件仍在连接设备并更新图像的问题,添加了设计模式检查

This commit is contained in:
zqm
2025-10-29 16:26:54 +08:00
parent fe3bd21124
commit 1b51115e4e
2 changed files with 45 additions and 3 deletions

View File

@@ -158,6 +158,7 @@ namespace JoyD.Windows.CS.Toprie
/// </summary> /// </summary>
public void StartCamera() public void StartCamera()
{ {
if (DesignMode) return;
try try
{ {
// 只有在没有接收图像时才启动 // 只有在没有接收图像时才启动
@@ -244,6 +245,7 @@ namespace JoyD.Windows.CS.Toprie
/// </summary> /// </summary>
private void StartReceiveImage() private void StartReceiveImage()
{ {
if (DesignMode) return;
try try
{ {
if (!_isReceivingImage && _deviceManager.ConnectionStatus == ConnectionStatus.Connected) if (!_isReceivingImage && _deviceManager.ConnectionStatus == ConnectionStatus.Connected)
@@ -265,6 +267,7 @@ namespace JoyD.Windows.CS.Toprie
/// </summary> /// </summary>
public void StopCamera() public void StopCamera()
{ {
if (DesignMode) return;
try try
{ {
if (_isReceivingImage) if (_isReceivingImage)
@@ -284,6 +287,7 @@ namespace JoyD.Windows.CS.Toprie
/// </summary> /// </summary>
private void DeviceManager_ImageReceived(object sender, ImageReceivedEventArgs e) private void DeviceManager_ImageReceived(object sender, ImageReceivedEventArgs e)
{ {
if (DesignMode) return;
Image image = null; Image image = null;
try try
{ {
@@ -449,6 +453,7 @@ namespace JoyD.Windows.CS.Toprie
/// </summary> /// </summary>
private void UpdateImageOnUI(Image image) private void UpdateImageOnUI(Image image)
{ {
if (DesignMode) return;
// 线程安全检查 - 确保在UI线程上执行 // 线程安全检查 - 确保在UI线程上执行
if (this.InvokeRequired) if (this.InvokeRequired)
{ {
@@ -606,6 +611,7 @@ namespace JoyD.Windows.CS.Toprie
/// </summary> /// </summary>
private void DeviceManager_ConnectionStatusChanged(object sender, ConnectionStatusChangedEventArgs e) private void DeviceManager_ConnectionStatusChanged(object sender, ConnectionStatusChangedEventArgs e)
{ {
if (DesignMode) return;
// 参数验证 // 参数验证
if (e == null) if (e == null)
{ {
@@ -680,6 +686,7 @@ namespace JoyD.Windows.CS.Toprie
/// </summary> /// </summary>
private void HandleConnectionStatusChanged(ConnectionStatusChangedEventArgs e) private void HandleConnectionStatusChanged(ConnectionStatusChangedEventArgs e)
{ {
if (DesignMode) return;
try try
{ {
// 确保在UI线程上更新UI状态 // 确保在UI线程上更新UI状态
@@ -787,6 +794,7 @@ namespace JoyD.Windows.CS.Toprie
/// <param name="isConnected">是否已连接</param> /// <param name="isConnected">是否已连接</param>
private void UpdateUIState(bool isConnected) private void UpdateUIState(bool isConnected)
{ {
if (DesignMode) return;
try try
{ {
// 根据连接状态更新图像框状态 // 根据连接状态更新图像框状态
@@ -830,6 +838,7 @@ namespace JoyD.Windows.CS.Toprie
/// </summary> /// </summary>
private void DeviceManager_ConnectionException(object sender, ConnectionExceptionEventArgs e) private void DeviceManager_ConnectionException(object sender, ConnectionExceptionEventArgs e)
{ {
if (DesignMode) return;
// 参数验证 // 参数验证
if (e == null) if (e == null)
{ {
@@ -924,6 +933,7 @@ namespace JoyD.Windows.CS.Toprie
/// </summary> /// </summary>
private void ShowError(string message) private void ShowError(string message)
{ {
if (DesignMode) return;
Console.WriteLine(message); Console.WriteLine(message);
// 可以根据需要添加UI上的错误显示 // 可以根据需要添加UI上的错误显示
@@ -968,6 +978,7 @@ namespace JoyD.Windows.CS.Toprie
/// </summary> /// </summary>
private void ErrorDisplayTimer_Tick(object sender, EventArgs e) private void ErrorDisplayTimer_Tick(object sender, EventArgs e)
{ {
if (DesignMode) return;
_errorDisplayTimer.Stop(); _errorDisplayTimer.Stop();
// 清除错误显示,恢复到等待图像状态 // 清除错误显示,恢复到等待图像状态
@@ -1007,6 +1018,7 @@ namespace JoyD.Windows.CS.Toprie
/// </summary> /// </summary>
private void ContextMenuStrip1_Opening(object sender, System.ComponentModel.CancelEventArgs e) private void ContextMenuStrip1_Opening(object sender, System.ComponentModel.CancelEventArgs e)
{ {
if (DesignMode) return;
try try
{ {
// 根据当前图像模式控制色彩模式菜单的可见性 // 根据当前图像模式控制色彩模式菜单的可见性

View File

@@ -151,7 +151,7 @@ namespace JoyD.Windows.CS.Toprie
public class DeviceManager : IDisposable public class DeviceManager : IDisposable
{ {
// 设计模式标志,用于在设计模式下跳过实际的设备连接和初始化 // 设计模式标志,用于在设计模式下跳过实际的设备连接和初始化
public static bool IsDesignMode { get; set; } = false; public static bool IsDesignMode { get; set; } = true;
// 项目路径,用于数据文件的存取 // 项目路径,用于数据文件的存取
private string _projectPath = ""; private string _projectPath = "";
@@ -1785,6 +1785,15 @@ namespace JoyD.Windows.CS.Toprie
/// </summary> /// </summary>
private void ReceiveImageDataWithHttpWebRequest() private void ReceiveImageDataWithHttpWebRequest()
{ {
// 在设计模式下,跳过实际的图像接收
if (IsDesignMode)
{
Log("设计模式下跳过实际的图像接收");
// 设置为不接收状态,避免持续运行
_isReceivingImages = false;
return;
}
Console.WriteLine($"图像接收线程启动: 使用HTTP请求获取图像数据"); Console.WriteLine($"图像接收线程启动: 使用HTTP请求获取图像数据");
try try
{ {
@@ -2670,6 +2679,13 @@ namespace JoyD.Windows.CS.Toprie
/// </summary> /// </summary>
private void ReceiveImageThread() private void ReceiveImageThread()
{ {
// 在设计模式下,跳过实际的图像接收
if (IsDesignMode)
{
Log("设计模式下跳过实际的图像接收");
return;
}
Console.WriteLine("警告: 已弃用的图像接收方式使用HTTP方式替代"); Console.WriteLine("警告: 已弃用的图像接收方式使用HTTP方式替代");
try try
{ {
@@ -3576,6 +3592,13 @@ namespace JoyD.Windows.CS.Toprie
/// <param name="state">定时器状态</param> /// <param name="state">定时器状态</param>
private void ReconnectCallback(object state) private void ReconnectCallback(object state)
{ {
// 在设计模式下,跳过实际的重连操作
if (IsDesignMode)
{
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实现原子操作检查防止重入
@@ -3993,6 +4016,13 @@ namespace JoyD.Windows.CS.Toprie
/// <param name="state">定时器状态</param> /// <param name="state">定时器状态</param>
private void HeartbeatCallback(object state) private void HeartbeatCallback(object state)
{ {
// 在设计模式下,跳过实际的心跳检测
if (IsDesignMode)
{
Log("设计模式下跳过实际的心跳检测");
return;
}
Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] HeartbeatCallback() - 开始执行"); Log($"[{DateTime.Now:HH:mm:ss.fff}] [线程:{Thread.CurrentThread.ManagedThreadId}] HeartbeatCallback() - 开始执行");
// 检查对象是否已释放 // 检查对象是否已释放