Exception : The calling thread cannot access this

2019-07-31 05:23发布

问题:

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)));


标签: c# wpf listbox