我在尝试操纵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