I am trying to call a method from BackgroundWorker
that adds a user control to ListView
:
private void AddItems(List<FileItem> fileItems) {
System.Threading.Thread.CurrentThread.SetApartmentState(System.Threading.ApartmentState.STA);
Dispatcher.BeginInvoke(new Action(() => files.ItemsSource = fileItems));
}
The user control files
is getting the data from fileItems
successfully in its constructor but it's throwing The calling thread must be STA, because many UI components require this.
exception. I already tried adding [STAThread]
attribute to all parent methods one by one but still it's throwing the exception. Where should I add this attribute?
UPDATE
Please also note that Dispatcher.BeginInvoke(new Action(() => files.Items.Clear()));
is executing correctly.