BackgroundWorker problem when accessing a List in DoWork

S

schnandr

Hi,

I have a user control which contains ListView. I copy all
ListViewItems into a separate List<ListViewItem> called
unfilteredItems.
I am using a BackgroundWorker to filter the ListView. When I access
this list in the DoWork Handler I am getting an
InvalidOperationException that
the List was not created by this thread. Is there a way to solve this
problem?

Thanks for every hint
 
S

schnandr

Here some Code

public partial class ListCtrl : UserControl
{
private IListCtrlFilter filter;

private BackgroundWorker filterWorker;

private List<ListViewItem> unfilterdItems = new
List<ListViewItem>();

...

void filterWorker_DoWork(object sender, DoWorkEventArgs e)
{

BackgroundWorker worker = sender as BackgroundWorker;

List<ListViewItem> unfiltered = e.Argument as
List<ListViewItem>;

if filter;!= null)
{
listView.Items.Clear();
foreach (ListViewItem item in unfiltered)
{
if (worker.CancellationPending)
return;

if (filter.Include(item))
worker.ReportProgress(0, item);
}
}
}


void filterWorker_ProgressChanged(object sender,
ProgressChangedEventArgs e)
{
listView.Items.Add(e.UserState as ListViewItem);
}

...
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top