I do the hard work with the background so that this does not affect my Silverlight UI thread. However, in the DoWork function, I get this exception:
UnauthorizedAccessException "Invalid access to multiple threads."
I know that I cannot access the user interface stream from BackgroundWorker. However, this exception occurs on this line:
ListBoxItem insert = new ListBoxItem();
How is this access to my u thread?
Here's the actual piece of code I narrowed it down. I basically do the job by creating listboxitems that I would like to insert into the list sourceList:
void FillSourceList()
{
busyIndicator.IsBusy = true;
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += (sender, args) =>
{
List<ListBoxItem> x = new List<ListBoxItem>();
for (int i = 0; i < 25; i++ )
{
ListBoxItem insert = new ListBoxItem(); //<---Getting exception here
insert.Content = "whatever";
x.Add(insert);
}
args.Result = x;
};
bw.RunWorkerCompleted += (sender, args) =>
{
foreach (ListBoxItem insert in (List<ListBoxItem>)(args.Result))
sourceList.Items.Add(insert);
busyIndicator.IsBusy = false;
};
bw.RunWorkerAsync();
}
source
share