调用线程无法访问该对象,因为另一个线程拥有它
尽管使用 Dispatcher.Invoke,我还是收到异常“调用线程无法访问此对象,因为不同的线程拥有它”。
代码如下: _btnImage1 是在 LaneImageReview 的 xaml 中声明的 ImageButton 的实例。请注意,我在下面的 RefreshLiveVESImage 方法中使用了 Dispatcher。
public partial class LaneImageReview : Page
{
private void RefreshLiveVESImages(VESImagePackageInfo p_VesImageInfo)
{
this._btnImage1.RefreshLiveVESImage(p_VesImageInfo);
}
}
public class ImageButton : Button
{
public void RefreshLiveVESImage(VESImagePackageInfo p_VesImageInfo)
{
BitmapImage bitmap = null;
try
{
//load background if not photo available
//if (p_Image == null)
//{
// _imgPhoto.Source = null;
//}
//else
//{
foreach (VESCameraInfo camInfo in p_VesImageInfo.VESCameras)
{
if (camInfo.CameraImageSets[0].FullImage != null)
{
bitmap = DVASViewController.GetBitmapImageFromByteArray(camInfo.CameraImageSets[0].FullImage.VESImage);
this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action<BitmapImage>(SetImageSource), bitmap);
break;
}
}
//}
}
catch (Exception ex)
{
SecurityController.CatchException(ex);
}
finally
{
}
}
private void SetImageSource(BitmapImage p_Image)
{
this.imgFrontLeft.Source = p_Image;
}
}
谢谢。
I am getting the exception "The calling thread cannot access this object because a different thread owns it" inspite of using Dispatcher.Invoke.
Below is the code: _btnImage1 is an instance of ImageButton declared in xaml of LaneImageReview. Note that I used Dispatcher in the RefreshLiveVESImage method below.
public partial class LaneImageReview : Page
{
private void RefreshLiveVESImages(VESImagePackageInfo p_VesImageInfo)
{
this._btnImage1.RefreshLiveVESImage(p_VesImageInfo);
}
}
public class ImageButton : Button
{
public void RefreshLiveVESImage(VESImagePackageInfo p_VesImageInfo)
{
BitmapImage bitmap = null;
try
{
//load background if not photo available
//if (p_Image == null)
//{
// _imgPhoto.Source = null;
//}
//else
//{
foreach (VESCameraInfo camInfo in p_VesImageInfo.VESCameras)
{
if (camInfo.CameraImageSets[0].FullImage != null)
{
bitmap = DVASViewController.GetBitmapImageFromByteArray(camInfo.CameraImageSets[0].FullImage.VESImage);
this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action<BitmapImage>(SetImageSource), bitmap);
break;
}
}
//}
}
catch (Exception ex)
{
SecurityController.CatchException(ex);
}
finally
{
}
}
private void SetImageSource(BitmapImage p_Image)
{
this.imgFrontLeft.Source = p_Image;
}
}
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以尝试类似的东西
you can try something like