473,387 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 4751
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Pro1712 | last post by:
Hi, this may be a stupid question: How can I can call the DoWork-function of a BackgroundWorker synchronous? Or in other words: How can I extend the BackgroundWorker class with a function...
1
by: ditnooitlezen | last post by:
Hi, the (.NET 2.0) backgroundworker object has a DoWork method that operates in a background thread. When the DoWork method is finished the RunWorkerCompleted event is raised in the parent...
1
by: Bob | last post by:
Hi, I am having trouble seeing how this bolts together. The UI starts a process which involves a long running database update. All Database activity is handled by a class called DT. DT has a...
9
by: =?Utf-8?B?VE9NX1Bhc2FkZW5h?= | last post by:
Hello, In my ASP.Net app I'm launching a BackgroundWorker thread in my Page_Load function. In that thread I'm attempting to connect to a SQL server using this connection string "Initial...
14
by: =?Utf-8?B?SXNobWFlbA==?= | last post by:
Hi, I have a form with a progress bar on it and wanted to use the BackgroundWorker to be able to update the progress. I looked at examples, run some of them, but in debug, when the code gets to...
5
by: Michael M. | last post by:
I have the following code (listed at bottom of post) that pings a small range of IP address to see which ones are alive. To speed things up a little I am trying to use more than one thread,...
0
by: Chris | last post by:
I would like to be able to pass the BackgroundWorker object and DoWork Event Args to a second function (third function?) and be able to still report the progress. I'm getting the following...
0
Plater
by: Plater | last post by:
So it occured to me that I told people to use this to solve some thread-safeing issues, but that I had not been using it myself (I had been using a delegate with invokerequired which seemed to lock...
0
by: PeterSchwennesen | last post by:
Problems starting a Timer Programmatically within a BackgroundWorker. I am trying to start a Timer inside a Backgroundworker. I want to start the BackGroundWorker and then have a timer tick a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.