2011-05-13 21 views
0

我得到異常「調用線程不能訪問此對象,因爲不同的線程擁有它」儘管使用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; 
    } 
} 

謝謝。

+0

哪條線是你得到的錯誤?它是否在實際的this.Dispatcher.Invoke行? – Tim 2011-05-13 18:38:01

回答

4

你可以嘗試像

 Application.Current.Dispatcher.Invoke((ThreadStart)delegate 
     { 
      DoWork(); 
     }); 
相關問題