调用线程无法访问此对象

调用线程无法访问此对象

本文介绍了WPF应用程序异常:调用线程无法访问此对象,因为另一个线程拥有它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我试图将控件添加到我的UserControl列表时,它抛出调用线程无法访问此对象,因为另一个线程拥有它。例外。





panel_PanelHolder.Children.Clear();

panel_PanelHolder.Children.Add(usr_panel) ;







但是当我使用下面的代码时,



Dispatcher.BeginInvoke(DispatcherPriority.Background,new Action(()=>

{

panel_PanelHolder.Children.Clear();

panel_PanelHolder.Children.Add(usr_panel);

}));





它抛出调用目标引发了异常。例外。

我需要的是清除控件,并向面板添加一个控件。我不确定如何尽快解决这个问题。我正在使用STA线程动态操作WPF窗口控件。有人可以帮我吗?

谢谢

While I was trying to add Controls to my UserControl list it threw "The calling thread cannot access this object because a different thread owns it." exception.


panel_PanelHolder.Children.Clear();
panel_PanelHolder.Children.Add(usr_panel);



But when I used below code,

Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
{
panel_PanelHolder.Children.Clear();
panel_PanelHolder.Children.Add(usr_panel);
}));


It throws "Exception has been thrown by the target of an invocation." exception.
What I need is to clear controls, and add a control to the panel. I''m not sure how to fix this ASAP. I''m using a STA thread to manipulate WPF window controls dynamically. Can someone help me on this?
Thanks

推荐答案


这篇关于WPF应用程序异常:调用线程无法访问此对象,因为另一个线程拥有它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 08:38