![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 368
Rep Power: 0
![]() |
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.
__________________
I am Addicted to Linux! |
|
|
|
|
|
#2 | |
|
Professional Programmer
Join Date: May 2006
Location: UK - London
Posts: 329
Rep Power: 3
![]() |
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.
__________________
Quote:
|
|
|
|
|
|
|
#3 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
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.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270 |
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 368
Rep Power: 0
![]() |
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.
__________________
I am Addicted to Linux! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Thread waiting. | equinox | Java | 3 | Mar 7th, 2008 5:51 PM |
| Accessing another thread | BlazingWolf | C# | 2 | Apr 19th, 2006 4:19 PM |
| Passing array as argument to a thread | Symptom | C | 5 | Sep 30th, 2005 6:52 PM |
| AhhH!!! I can't find anything on this exception... | stakeknife | ASP | 2 | Sep 26th, 2005 7:48 AM |