异常:调用线程无法访问此对象,因为其他线程拥有它. [英] Exception : The calling thread cannot access this object because a different thread owns it.

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

问题描述

可能重复:
调用线程无法访问此对象对象,因为其他线程拥有它

我正在WPF项目(带有C#的WPF)中制作一个登录表单

I am making a Login Form in My WPF Project (WPF with C#)

我有一种主要形式,名称是 Window1 .当我启动应用程序时,将首先显示带有 Username Password 字段的登录对话框"表单.如果 Username Password 是正确的,则显示 MainWindow1 .

I have one main form whose name is Window1. When I start the app a Login Dialog Form is first shown with Username and Password fields. If Username and Password are correct, then show MainWindow1.

我的 Window1 有一个 ListBox 需要填充.

My Window1 has one ListBox which is to be filled.

应用程序成功登录后,必须将Item添加到 Window1 中的 ListBox 控件中.但是显示了异常.

When the app succeed in login, it has to add Item to ListBox control in Window1. But An Exception is shown.

例外:

由于另一个线程拥有它,因此调用线程无法访问该对象.

The calling thread cannot access this object because a different thread owns it.

我不知道我的 ListBox 控件怎么了?

I don't know what happen to my ListBox control?

推荐答案

您必须使用MethodInvoker

You have to use MethodInvoker

listBox.Invoke((MethodInvoker)(() => listBox.Items.Add(item)));

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

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆