Exception : The calling thread cannot access this object because a different thread owns it. [duplicate]

六月ゝ 毕业季﹏ 提交于 2019-12-11 06:23:59

问题


Possible Duplicate:
The calling thread cannot access this object because a different thread owns it

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

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.

My Window1 has one ListBox which is to be filled.

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

Exception :

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

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


回答1:


You have to use MethodInvoker

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


来源:https://stackoverflow.com/questions/10764747/exception-the-calling-thread-cannot-access-this-object-because-a-different-thr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!