BackgroundWorker problem when accessing a List in DoWork

  • Thread starter Thread starter schnandr
  • Start date Start date
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
 
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);
}

...
}
 
Back
Top