2011-06-21 35 views
1

我有一個方法(CreateDocument)在最後觸發事件。事件參數包含一個FixedDocument。在我的主窗口代碼中,我試圖像設置的DocumentViewer的文檔:「因爲不同的線程擁有它調用線程不能訪問該對象」從另一個線程設置DocumentViewer.Document?

void lpage_DocCreated(object sender, LabelDocumentEventArgs e) 
{ 
    this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, 
    new DispatcherOperationCallback(delegate 
    { 
    FixedDocument fd = e.doc; 
    documentViewer1.Document = fd; 
    documentViewer1.FitToWidth(); 
    return null; 
    }), null); 
} 

我收到在線documentViewer1.Document = fd;

我能更新另一個事件處理的進度條,同樣的方法火災而它的工作:

Int32 progress = Int32.Parse(sender.ToString()); 
    progBar.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, 
     new DispatcherOperationCallback(delegate 
     { 
      progBar.Value = progress; 
      return null; 
     }), null); 

我想不通爲什麼我不能設置文檔時,我當我設置進度欄值時,m本質上是做同樣的事情。

回答

0

FixedDocument元素也具有線程親和性。所以如果你在一個單獨的線程中創建它,而不是documentViewer1,那麼你會得到這個異常。

基本上,任何派生自DispatcherObject的東西都具有線程親和性。 FixedDocument派生自DispatcherObject,就像查看器控件一樣。