I'm making a downloader application. There are main window and downloader classes named Main.xaml.cs and Downloader.cs in my project.
There is a custom ListBox in the main window. I'm trying to refresh the Listbox item from the Downloader.cs, but the application gives the "The calling thread cannot access this object because a different thread owns" error.
Downloader.cs:
namespace MyDownloaderApp
{
class Downloader
{
/*
...
*/
private void doWork()
{
((MainWindow)System.Windows.Application.Current.MainWindow).myListBox.Items.Refresh();
}
}
}
I got the following error:
An exception of type 'System.InvalidOperationException' occurred in WindowsBase.dll but was not handled in user code: "The calling thread cannot access this object because a different thread owns it."
What is causing this error and how can I fix it?