"update code to update the thread here" was a typo :)
Main Topics
Browse All TopicsHi there,
I've got a ListView that I want to populate with thousands of records from a database. I'd like to do this in another thread so the user can modify certain criteria while the thread is still running. Any modifications would stop that thread, clear the list view and restart the thread.
I'm a little unsure how to do this, and if it is okay accessing the list view control from another thread. Any help would be greatly appreciated.
Many thanks.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
So would I perform the database fetching and list view filling in the UpdateListView function. And would that method be running asynchronously so user interaction with the form is still functional? What does the Me.InvokeRequired mean?
How would you go about safely aborting and restarting the thread?
This will not improve the filling process but allows the user work in the form while the list is updating. You must also add an Application.DoEvents after each line insert.
It's not usual and not practival to add some many records to the listview, but if you want to improve the performance, you can fill in virtual mode. Check some information:
http://www.codeproject.com
http://
@jpaulino...your code there isn't multithreading UpdateListView() at all! =\
You create a secondary thread in lvThreadMain()...so far so good.
But then you told the secondary thread to Invoke UpdateListView() using "Me" as the source. The code basically says:
Find the thread that created "Me" (the Form), and then run the delegate I pass (UpdateListView) on that same thread.
So the end result is that UpdateListView() is back running on the main UI thread!
This is quite evident if you change your code to:
(click the button then continuosly drag the form around by the title bar as you watch the debug messages)
But if you use a Thread to update the ListView in background, it will blink the ListView Items, because you're updating the UI several times. I think that using Virtual Mode it's best solution for this OR fill the list with less items.
Here's an corrected version of the thread, but it blinks the Listview. :(
@Idle_Mind,
Is there any way to correct this blink ?
Yah...not sure if there is a way to prevent the flickering when adding on the fly like that. It's not recommended anyhoo since adding items constantly isn't much better than tying up the UI thread in the first place.
The virtual mode suggestion looks great...will have to play with it. =)
(If you don't have .Net 2.0 or higher then you could only load a small subset of the records at once and then provide "next/previous" page buttons for the user...basically a manual implementation of the virtual mode)
Business Accounts
Answer for Membership
by: jpaulinoPosted on 2009-06-22 at 03:16:38ID: 24680975
Here is an example how to start a thread, using a delegate, to allow you to fill the listview.
You can abort the thread if you want and clean the itens of the user do anything.
Select allOpen in new window