调用线程无法访问此对象,因为另一个线程拥有它,即使在使用调度程序时也是如此
The calling thread cannot access this object because a different thread owns it, even when using the dispatcher
我有一个 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.
我该如何解决这个问题?
从我看到的几行代码来看,我会尝试在图形线程上创建所有图形对象,包括 Run
对象:
Dispatcher.Invoke(new Action(() => {
Run r = new Run((string)name + Environment.NewLine);
r.Foreground = Brushes.Green;
currentlyOnlineParagraph.Inlines.Add(r);
}), DispatcherPriority.ContextIdle);
我有一个 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.
我该如何解决这个问题?
从我看到的几行代码来看,我会尝试在图形线程上创建所有图形对象,包括 Run
对象:
Dispatcher.Invoke(new Action(() => {
Run r = new Run((string)name + Environment.NewLine);
r.Foreground = Brushes.Green;
currentlyOnlineParagraph.Inlines.Add(r);
}), DispatcherPriority.ContextIdle);