471,123 Members | 869 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,123 software developers and data experts.

BackgroundWorker problem when accessing a List in DoWork

Hi,

I have a user control which contains ListView. I copy all
ListViewItems into a separate List<ListViewItemcalled
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?
Here some Code

public partial class ListCtrl : UserControl
{
private IListCtrlFilter filter;

private BackgroundWorker filterWorker;

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

...

void filterWorker_DoWork(object sender, DoWorkEventArgs e)
{

BackgroundWorker worker = sender as BackgroundWorker;

List<ListViewItemunfiltered = 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);
}

...
}

Thanks for every hint
Nov 16 '08 #1
1 4575
On Sat, 15 Nov 2008 23:06:09 -0800, schnandr
<an***************@skillworks.dewrote:
Hi,

I have a user control which contains ListView. I copy all
ListViewItems into a separate List<ListViewItemcalled
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?
Surely your error is complaining that the _ListView_ was not created by
the same thread. There should be no problem accessing an instance of
List<Tfrom a different thread than the one used to create it.

As for addressing the problem, the most obvious solution given that you're
using a BackgroundWorker is to use exclusively the BackgroundWorker events
to access the ListView itself. For example, don't clear the list until
the first time the ProgressChanged handler is called. Alternatively, you
can use Control.Invoke() if you really feel the need (but then it begs the
question as to why you're bothering with BackgroundWorker :) ).

Search this newsgroup for terms like "cross-thread exception",
"control.invoke", and "backgroundworker" for more details.

Pete
Nov 16 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Pro1712 | last post: by
1 post views Thread by ditnooitlezen | last post: by
9 posts views Thread by =?Utf-8?B?VE9NX1Bhc2FkZW5h?= | last post: by
14 posts views Thread by =?Utf-8?B?SXNobWFlbA==?= | last post: by
reply views Thread by Chris | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.