我在嘗試操縱WPF中的FlowDocumentScrollViewer控件時遇到了問題。我通過互聯網搜索了這種錯誤,但發現我的問題非常獨特。交叉線程操縱WPF控制錯誤:不能
這是代碼:
delegate void delg_FlowDoc(FlowDocument fdoc);
//Function provided for both internal calling and outside calling
public void setfDocDisplay(FlowDocument fdoc)
{
FlowDocumentScrollViewer display = this.fDocDisplay; //control to be operated
if (display.Dispatcher.CheckAccess())
{
//called from own thread, just operate directly!
string debug_check_ThreadName = Thread.CurrentThread.Name;
if (debug_check_ThreadName == "Thread_MainUI")
{
//Debug only, now current thread MUST be the thread
//created the UI!!!!
display.Document = fdoc; //<-- Error this line
}
}
else
{
//called from other threads, use Invoke()!
delg_FlowDoc pFun = new delg_FlowDoc(setfDocDisplay);
this.Dispatcher.Invoke(pFun, new object[] { fdoc });
//display.Dispatcher.Invoke(pFun, new object[] { fdoc });
//this.Dispatcher.BeginInvoke(pFun, new object[] { fdoc });
}
}
並且符合display.Document = fdoc;
發生了錯誤,說仍無法從不同的線程訪問控制。
這是錯誤的詳細快照:
我已經執行
display.Dispatcher.CheckAccess()
,以確保當前線程的控制顯示屏上操作權限:,因爲我很困惑。
我再次添加一個
debug_check_ThreadName
翻一番確保它在那一刻正確的線程。我想正好在另一個但規模較小的WPF項目相同的代碼是免費的錯誤
,但仍然得到了同樣的問題告訴我,我嘗試從不同的線程操作。
我不熟悉的調度或其他屬性調用等特性,所以我不知道現在解決這個問題。
如果誰能給我這個錯誤的任何暗示?
謝謝!
[解決]
謝謝500 - 內部服務器錯誤!
因爲我仍然由事實震驚了,我決定把我的問題的理解,以方便一些其他人有同樣的問題,看到了這個問題。
我現在已經解決了這一問題。這是因爲的FlowDocument FDOC實際上是一個類型的UI控件,並在另一個線程創建的。所以當主窗口線程想操作FDOC - 雖然它有權FlowDocumentScrollViewer顯示 - 它沒有權限在此分配句話其他控制01FDOC。
這是相當奇怪的!我認爲fdoc只是一個像字符串或int變量的正常變量...> <
和FlowDocument不凍結,所以你不能分享它 – 2012-02-02 22:23:56
?你能給出更具體的解釋嗎?謝謝:) – JXITC 2012-02-02 22:26:43
源文件與創建它的線程綁定在一起 - 就像在這方面的UI控制。 – 2012-02-02 22:27:36