移除RealTimeImage属性的线程安全检查:作为后台变量,不直接操作UI,移除InvokeRequired相关代码以简化实现并提高性能,保留状态检查和资源管理逻辑
This commit is contained in:
@@ -102,46 +102,16 @@ namespace JoyD.Windows.CS
|
||||
private Image mRealTimeImage = null;
|
||||
/// <summary>
|
||||
/// 实时图像属性
|
||||
/// 作为后台变量,移除线程安全检查以提高性能
|
||||
/// 注意:调用方需确保在合适的线程上下文中访问
|
||||
/// </summary>
|
||||
public Image RealTimeImage
|
||||
{
|
||||
get
|
||||
{
|
||||
// 读取操作也需要线程安全检查
|
||||
if (this.InvokeRequired)
|
||||
{
|
||||
return (Image)this.Invoke(new Func<Image>(() => mRealTimeImage));
|
||||
}
|
||||
return mRealTimeImage;
|
||||
}
|
||||
set
|
||||
{
|
||||
// 线程安全检查 - 确保在UI线程上执行
|
||||
if (this.InvokeRequired)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 使用Action委托设置值,避免闭包问题
|
||||
this.BeginInvoke(new Action<Image>(SetRealTimeImageInternal), value);
|
||||
}
|
||||
catch (ObjectDisposedException)
|
||||
{
|
||||
// 控件已释放,释放传入的图像资源
|
||||
value?.Dispose();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// 在UI线程上直接调用内部方法
|
||||
SetRealTimeImageInternal(value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 内部方法,在UI线程上安全地设置实时图像
|
||||
/// </summary>
|
||||
/// <param name="value">要设置的新图像</param>
|
||||
private void SetRealTimeImageInternal(Image value)
|
||||
{
|
||||
// 防止重复设置相同的图像
|
||||
if (mRealTimeImage == value)
|
||||
@@ -175,3 +145,4 @@ namespace JoyD.Windows.CS
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user