using Listview vs ListBox in a backgroundWorker thread

R

Rich P

In a backgroundworkder thread in the dowork event I can retrieve a row
(item) of data from a listbox (in a for loop). But if I try to retrieve
the same data from a listview I get a crossthread error. Is there a
workaround for this besides placing my for loop outside of the
backgroundworker (which means I would be calling the backgroundwoker
thousands of times) ?

Rich
 
P

Peter Duniho

In a backgroundworkder thread in the dowork event I can retrieve a row
(item) of data from a listbox (in a for loop). But if I try to retrieve
the same data from a listview I get a crossthread error.

Trying to access a ListBox instance from a thread other than where that
ListBox instance was created should also cause the cross-thread
exception. If it doesn't, that's a bug in .NET.
Is there a
workaround for this besides placing my for loop outside of the
backgroundworker (which means I would be calling the backgroundwoker
thousands of times) ?

Cross-thread exceptions are generally addressed using the Control.Invoke()
or Control.BeginInvoke() method. You should use that whether you are
accessing a ListView or ListBox object from your BackgroundWorker.DoWork
event handler.

Unless you post a concise-but-complete code example that demonstrates
exactly your problem, that's about as specific an answer as is worth
writing.

I will point out however that generally the flow of information is _from_
the BackgroundWorker thread to the main GUI thread, and not the other
way. If you are pulling data _from_ a GUI object during the actual
processing within your BackgroundWorker thread, it's likely there's a more
fundamental design flaw going on. Again, without actual code to comment
on, it's impossible to say for sure. But the general description you've
given suggests a need to fix the basic design, rather than to specifically
address the cross-thread exception issue.

Pete
 
R

Rich P

I will post the trouble code a little bit later (getting kicked out of
the office for the weekend right now :).

Rich
 
P

Peter Duniho

As a quick and temporary solution you can do the below.
Call below before you start worker thread (or in your Form's
constructor).

e.g (C# code)

CheckForIllegalCrossThreadCalls = false;

No. Do not ever do this. It simply disables the error. It doesn't
nothing at all to make the code more correct.
 
R

Registered User

In a backgroundworkder thread in the dowork event I can retrieve a row
(item) of data from a listbox (in a for loop). But if I try to retrieve
the same data from a listview I get a crossthread error.
The same error should occur for both controls. UI controls and non-UI
threads don't play well together. This is a good thing.
Is there a
workaround for this besides placing my for loop outside of the
backgroundworker (which means I would be calling the backgroundwoker
thousands of times) ?

Yep. The list control is populated with a collection of something.

List<SomeType> list = getDataFromSomewhere();
someListControl.ItemsSource = list;

To have a background worker use that list, pass the list as an
argument

worker.RunWorkerAsync(list);

To access the list in the DoWork method

.... _DoWork(object sender, DoWorkEventArgs e)
{
List<SomeType> list = e.Argument as List<SomeType>;
if (list != null)
{
iterate the list and do what needs to be done
}
}

regards
A.G.
 
P

Pasan Indeewara

Pete,
I agree with you. It's a bad programming practice provided for
backward compatibility with VS 2003 I think.
 

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