2015-11-01 30 views
0

我有一個Paragraph對象,我嘗試從與創建它不同的線程訪問,然後發佈我的問題在此處搜索一個在線解決方案,我發現了「Dispatcher」解決方案,這對我來說不管用。調用線程無法訪問此對象,因爲不同的線程擁有它,即使在使用調度程序時

下面的代碼:

Run r = new Run((string)name + Environment.NewLine); 
r.Foreground = Brushes.Green; 
Dispatcher.Invoke(new Action(() => { currentlyOnlineParagraph.Inlines.Add(r); }), DispatcherPriority.ContextIdle); 

我得到這個錯誤:

InvalidOperationException was unhandled 
An unhandled exception of type 'System.InvalidOperationException' occurred in mscorlib.dll 
Additional information: The calling thread cannot access this object because a different thread owns it. 

screenshot

我怎樣才能解決這個問題?

回答

2

從代碼,我看到了幾行字,我會嘗試在圖形線程上創建的所有圖形對象,包括Run對象:

Dispatcher.Invoke(new Action(() => { 
    Run r = new Run((string)name + Environment.NewLine); 
    r.Foreground = Brushes.Green; 
    currentlyOnlineParagraph.Inlines.Add(r); 
}), DispatcherPriority.ContextIdle); 
+0

謝謝,它的工作! –

+0

歡迎你。我很高興它給出了修復 –

相關問題