Populating listbox with thread event

B

Beren

Hello,

I'm having a thread that retrieves data from a website, stores it into an
array, and raises an event in the form thread to populate a listbox.
As soon as the first item is to be added (even if it's no part of the array,
but something random) my application is irresponsive for some seconds.

I pass the array to the event handler in the eventargs. Something like :

sub threadproc
while bMorePages()
getResultsFromNextPage()
fireEvent(myarray)
end while
end sub

sub fireEvent(myarray)
dim args as new eventargs(myarray)
raiseEvent(Me,args)
end sub

' // And in the event handler on the form
' // it goes a bit like :
' // e = eventargs
sub eventhandler()
synclock Me
Dim li as listviewitem
while i < e.myarray.length
application.doevents()
li = new listviewitem()
li.text = e.myarray(i)
'// freezes on i = 0
myListbox.items.add(li)
'// continues
i += 1
end while
end synclock
end sub


While debugging I noticed the application freezes when the first item is
added to the listview
As you can see, the thread continues as long as there are more pages
(bMorePages = True), and never freezes again in this cycle,
only when the very first item is added in the listbox.

I get accurate results, but the add command freezing the application is
terribly annoying.
Can anyone help ?

Thanks,

Beren
 
K

Ken Tucker [MVP]

Hi,

I see you are storing your data in an array. Then you convert them
to listviewitems and add them one at a time. Why dont you store your data
in an array of listviewitems and use lmyListbox.items.addrange(myarray) to
add them all at once.

Ken
 
B

Beren

Thanks alot m8 ! I'll have a try tonight :)

Jakob Christensen said:
thread "owning" the form and you should not use SyncLock when calling into a
form
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html
/vaconfreethreadingwithformscontrols.asp).listbox since the eventhandler is called from the other thread. I recommend
reading Chris Sells' articles on this very issue:
http://msdn.microsoft.com/library/d.../html/vaconfreethreadingwithformscontrols.asp
 

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