在运行时添加了动态控件,线程.由于其他线程拥有该对象,因此调用线程无法访问该对象. [英] Dynamic controls added at runtime, threading, The calling thread cannot access this object because a different thread owns it.
问题描述
我的问题是在加载时,我初始化了一个新线程.此线程创建一个新控件,然后尝试将控件添加到stackapnel中,并且我收到的错误是-
My problem is on load I initialise a new thread. This thread create a new control then tries to add the control to a stackapnel and the error I receive is -
由于另一个线程拥有它,因此调用线程无法访问该对象.
The calling thread cannot access this object because a different thread owns it.
这是我的代码e-
线程
t = 新 线程 ( ThreadStart (
t = new Thread(new ThreadStart(delegate { Create(); }));
t.IsBackground =
t.IsBackground =
true ;
true;
t.SetApartmentState(
t.SetApartmentState(
ApartmentState .STA);
ApartmentState.STA);
t.Priority =
t.Priority =
ThreadPriority .最高;
ThreadPriority.Highest;
t.Start();
t.Start();
公开
void Create()
void Create()
{
//创建控件
pivControl =
pivControl =
新 DevExpress.Xpf.PivotGrid. PivotGridControl ();
new DevExpress.Xpf.PivotGrid.PivotGridControl();
Test.Dispatcher.Invoke(
Test.Dispatcher.Invoke(
新 动作 (() =>
new Action(() =>
{
StackPanel1.Children.Add(pivControl);
StackPanel1.Children.Add(pivControl);
}));
}
感谢您的帮助.
推荐答案
为您弄清楚事情.
To make things clearer for you.
Test.dispatcher.Invoke
Test.dispatcher.Invoke
实际上应该是
Stackpanel1.dispatcher.Invoke
Stackpanel1.dispatcher.Invoke
对此表示抱歉.
这篇关于在运行时添加了动态控件,线程.由于其他线程拥有该对象,因此调用线程无法访问该对象.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!