Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 20th, 2008, 8:18 PM   #1
King
Professional Programmer
 
King's Avatar
 
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 376
Rep Power: 0 King is an unknown quantity at this point
Question 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.
__________________
I am Addicted to Linux!
King is offline   Reply With Quote
Old Apr 21st, 2008, 4:06 AM   #2
kruptof
Professional Programmer
 
kruptof's Avatar
 
Join Date: May 2006
Location: UK - London
Posts: 330
Rep Power: 3 kruptof is on a distinguished road
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:
When I was young it seemed that life was so wonderful,a miracle, oh it was beautiful, magical.
Now watch what you say or they'll be calling you a radical,a liberal, oh fanatical, criminal. Oh won't you sign up your name,we'd like to feel you're acceptable, respectable, oh presentable, a vegetable
kruptof is offline   Reply With Quote
Old Apr 21st, 2008, 4:03 PM   #3
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
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
Dameon is offline   Reply With Quote
Old Apr 21st, 2008, 7:27 PM   #4
King
Professional Programmer
 
King's Avatar
 
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 376
Rep Power: 0 King is an unknown quantity at this point
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!
King is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:39 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC