2010-01-22 56 views
3

我已經構建了一個顯示圖像的WPF控件。現在我想以非常快的速度改變這張圖片。 我已經構建了一個ImageContainer類來保存圖像,並有一個ChangedEventHandler,它在更改後更新控件中的圖像。WPF中的圖像更新TargetInvocationException

被執行的代碼看起來是這樣的:

videoImageThread = new Thread(
      new ThreadStart(
       delegate() 
       { 
        this.VideoCapture.Dispatcher.Invoke(
        System.Windows.Threading.DispatcherPriority.Normal, 
        new Action(
         delegate() 
         { 

          videoImage.Source = VideoImageContainer.Instance.VideoBitmapSourceImage; 

         } 
       )); 
       } 
     )); 


private void Instance_VideoRefresh() 
    { 
     if (VideoImageContainer.Instance.VideoImage != null) 
     { 
      lock (videoImageSetLock) 
      { 
       videoImageThread.Start(); 
      } 
     } 
    } 

此代碼拋出一個System.Reflection.TargetInvocationException,我究竟做錯了什麼?

+0

難道是因爲你正在訪問屬於另一個線程調度器? – Andres 2010-01-22 21:52:28

+0

但那不是重點嗎?我不能從控制線程以外的線程更新控件,如果我這樣做,我會得到另一個異常。這就是爲什麼我有這個代碼,它被一些工作者線程調用並鏈接到控制線程 – 2010-01-22 21:56:47

+2

查看異常的InnerException屬性以找到真正的原因。 – 2010-01-22 22:17:57

回答

1

在我看來,你喜歡你正在調用一個線程來調用一個線程?!

你試圖調用的調度員直接像這樣的動作:

private void Instance_VideoRefresh() 
{ 
    if (VideoImageContainer.Instance.VideoImage != null) 
     this.VideoCapture.Dispatcher.Invoke(
       System.Windows.Threading.DispatcherPriority.Normal, 
       new Action(
        delegate() 
        { 
         videoImage.Source = VideoImageContainer.Instance.VideoBitmapSourceImage; 
        } 
      )); 
} 
0

您是否嘗試過簡單地將videoImage.Source綁定到一個屬性,然後在您的Instance_VideoRefresh方法中更改該屬性?

我試過以前用圖像/列表<ImageSource> /定時器組合,它工作得很好。