The calling thread cannot access this object because a different thread owns it

瘦欲@ 提交于 2019-12-11 15:05:50

问题


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.


回答1:


you can try something like

     Application.Current.Dispatcher.Invoke((ThreadStart)delegate
        {  
           DoWork();
        });


来源:https://stackoverflow.com/questions/5996162/the-calling-thread-cannot-access-this-object-because-a-different-thread-owns-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!