threads / queues and listview

  • Thread starter Thread starter Josh Booth
  • Start date Start date
J

Josh Booth

Hi,

I have a large amount of data to display in a listview so I decided I would
fire up a new thread and share a queue between the thread and the main app
using lock( this.m_listQueue.SyncRoot ) where needed. I gave the thread a
deligate as well so it could call for the UI to be updated every 100 records
or so.

Now it all works fine, the list is populated correctly and in sets of 100
records at a time. However, if the user taps the list before it has
completly loaded the program hangs, no exceptions appear to be thrown or
anything. All the listview.Add calls occure in the main application thread
and all dequeue and enqueue operations are done in locks. Does anybody have
any idea what is causing this problem??

Regards
Josh
 
Hi,

Create an event handler that does the update of the ListView. Then, in your
delegate (this is executing in the thread context, not the UI), call the
event handler. Here is the syntax (pseudo VB) that I use:

'create a Private structure that holds the data to be updated

Me.Invoke(New EventHandler(AddressOf DisplayData))

End Sub

Private Sub DisplayData(ByVal sender As Object, ByVal e As EventArgs)

'This marshals receive data from the thread context (OnComm) to the Windows
Form STAThread context

With myListView

'use the data in the structure to update the list

End With

End Sub

This is equivalent (sorta) to using Control.Invoke, or BeginInvoke in the
desktop framework to marshal data from a thread to the UI apartment.

Dick

--
Richard Grier (Microsoft Visual Basic MVP)

See www.hardandsoftware.net for contact information.

Author of Visual Basic Programmer's Guide to Serial Communications, 3rd
Edition ISBN 1-890422-27-4 (391 pages) published February 2002.
 
Back
Top