![]() |
Cross-Thread Exception
I want to update a listview on a separate thread so the window does not lock if there is a lot to populate. The problem with this is that I get an exception being thrown because the thread updating the listview is not the thread that created the control. Does anyone know a decent way around this. After googling, most of the answers to this is to use delegates, but it ends up just being called by the main thread which defeats the whole purpose because the window locks again. I have also tried backgroundworker's, but I get the same cross-thread exception. This is getting quite frustrating.
|
Re: Cross-Thread Exception
How are you getting the data being added to the listview, is it by network or disk. If so load the data into memory first then add it to the list. Also since you're in the main thread why not have an progress bar or something similar indicating there is progress.
|
Re: Cross-Thread Exception
Adding elements to the list view won't take long. And you'll have to do it in the main thread.
I can think of two reasons that populating the listview may be too slow. You are adding items one by one without using Begin/EndUpdate (to prevent pointless redrawing and processing). or... You are loading items from disk, network, etc. and adding all in one step. Disk access and such is very slow compared to adding to a listview. You wouldn't want to do that in your main thread. Start a background thread. Load all of the items into an array list or similar. Then, using Invoke, call AddRange to add all items in one go. Begin/EndUpdate would probably be unneeded - I can't remember if AddRange takes care of that, but it probably does. |
Re: Cross-Thread Exception
I am loading a dataset from a database and then populating the listview with it, but I am looping through adding the records one at a time. I did not know there was a faster way of doing this.
|
| All times are GMT -5. The time now is 4:15 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC