每当我刷新标签时,我都会收到此错误: The calling thread cannot access this object because a different thread owns it. 我尝试使用WPF表单调用但是它是's failed. I' m .
delegate void lostfocs(string st);
private void imgPayment_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Thread t = new Thread(modi);
t.Start();
}
void modi()
{
try
{
label1.Content = "df";
}
catch
{
lostfocs ld = new lostfocs(up);
// ld.Invoke("df");
object obj=new object();
ld.Invoke("sdaf");
}
}
void up(string st)
{
label1.Content = st;
}
6 回答
使用Dispatcher.Invoke方法 .
也
您收到错误是因为您的标签是在UI线程上创建的,并且您正在尝试通过另一个线程修改其内容 . 这是您需要Dispatcher.Invoke的地方 .
看看这篇文章WPF Threads Build More Responsive Apps With The Dispatcher
您可以使用Dispatcher . 你的代码变成......
使用Dispatcher.Invoke
例
我启动了一个非UI线程,在这个线程中我也盯着一个UI线程 . 所以我的要求就像在非UI线程中运行UI线程 . 处理此方案时,我得到以下异常 . "Exception: The calling thread cannot access this object because a different thread owns it."
在这种情况下,我使用了UI元素的Dispatcher.Invoke方法,如下所示,它运行良好 .
有几个使用BeginInvoke的建议,但没有提到EndInvoke . 好的做法是'每个BeginInvoke都有一个匹配的EndInvoke',当然还需要有一些防范竞争条件的保护措施(想想:多个BeginInvoke代码会发生什么,但是还没有完成处理?)
它已经在MSDN示例和WinForms上发布的书籍中看到了这个错误(并且,是的,这是一个错误)