tags:

views:

57

answers:

1

Hello,

I've got a bit of a problem, I'm using a backgroundworker to do a lot of processing and it adds items to a listview with:

AddListItem(ListView1, listItem)

Here is the delegate code to send the command to the listview outside of the thread:

Delegate Sub AddListItem_Delegate(ByVal [ListView] As ListView, ByVal [text] As Object)
Private Sub AddListItem(ByVal [ListView] As ListView, ByVal [text] As ListViewItem)
    If [ListView].InvokeRequired Then
        Dim MyDelegate As New AddListItem_Delegate(AddressOf AddListItem)
        Me.Invoke(MyDelegate, New Object() {[ListView], [text]})
    Else
        ListView1.Items.Add([text])
    End If
End Sub

The problem is, as you might imagine, flickering of the listview. Can anyone help me out with a solution to execute a LockWindowUpdate(Me.Handle) command in the backgroundworker? I've tried creating a new delegate but it's not working (errors, I don't understand vb.net enough).

Thanks!

A: 

Be sure not to call this code too frequently, it is very detrimental to the health of the UI thread. If you call it more than about 1000 times per second then the UI thread will stop responding completely. It won't redraw anymore nor respond to mouse clicks.

If you have a large number of items to add then make sure you first store them in a List(Of ListViewItem), then Invoke() and add them to the list view, bracketed by BeginUpdate() and EndUpdate(). There's no point in LockWindowUpdate().

That will take care of most flicker, but won't eliminate it completely. The ListView class supports double buffering but it isn't turned on by default. To fix that, add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox, replacing the existing ListView.

Public Class MyListView
    Inherits ListView

    Public Sub New()
        Me.DoubleBuffered = True
    End Sub
End Class
Hans Passant
great, will try this out and thanks!
Joe