I have a problem with the Listview control where if I open a program, populate a Datareader and then loop the reader into a Listview, it's very fast the first time (400 milliseconds for 600 items in 5 columns) and then if you click it again for a refresh where it re-executes the datareader, etc, it will take 35 seconds. After the first use of the List view, it's always slow.
I figured it had to do with repainting the list, so I set the Visible property to False while it loops. No better. I set the BeginUpdate and EndUpdate accordingly and that doesn't help. I tried to use:
Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long
but I can't get a handle for the control that works with the call (I didn't spend much time on this, I doubt it will help). I also SuspendLayout and ResumeLayout.
The only thing I can think of yet is to create the Listview in code and destroy it after each use. Would this work, and if not, what else? Should I just bite the bullet and write it with some other control
Me.Cursor = Cursors.WaitCursor
'load the list view columns
lvwEvents.Clear()
lvwEvents.SuspendLayout()
lvwEvents.Columns.Add("EventLogID", 0, HorizontalAlignment.Left)
lvwEvents.Columns.Add("Type", 100, HorizontalAlignment.Left)
lvwEvents.Columns.Add("TimeGenerated", 150, HorizontalAlignment.Left)
lvwEvents.Columns.Add("SourceName", 125, HorizontalAlignment.Left)
lvwEvents.Columns.Add("Message", 500, HorizontalAlignment.Left)
lvwEvents.Columns.Add("Category", 70, HorizontalAlignment.Left)
If Not bEmpty Then
drSQL = cCommonData.ExecuteSQL_GetReader(strSQL)
Dim fColor As System.Drawing.Color
Dim Litem As ListViewItem
Dim flag As Boolean = False
Me.lvwEvents.Visible = False
Dim start As System.DateTime = System.DateTime.Now
Dim finish As System.DateTime
Dim i As Integer
lvwEvents.BeginUpdate()
While drSQL.Read
i = i + 1
Litem = New ListViewItem
Litem.Text = (drSQL.Item("EventLogID"
.ToString())
Litem.SubItems.Add(drSQL.Item("Type"
.ToString(), fColor, Color.White, SystemInformation.MenuFont)
Litem.SubItems.Add(drSQL.Item("TimeGenerated"
.ToString(), fColor, Color.White, SystemInformation.MenuFont)
Litem.SubItems.Add(drSQL.Item("SourceName"
.ToString, fColor, Color.White, SystemInformation.MenuFont)
Litem.SubItems.Add(drSQL.Item("Message"
.ToString, fColor, Color.White, SystemInformation.MenuFont)
Litem.SubItems.Add(drSQL.Item("Category"
.ToString, fColor, Color.White, SystemInformation.MenuFont)
lvwEvents.Items.Add(Litem)
End While
lvwEvents.EndUpdate()
finish = Date.Now
Dim span As System.TimeSpan = finish.Subtract(start)
Console.WriteLine(EventLogType & "(" & i & "
: " & span.Seconds.ToString() & "." & span.Milliseconds.ToString())
' Close and Clean up objects
drSQL.Close()
End If
Me.lvwEvents.Tag = Nothing
Me.lvwEvents.ListViewItemSorter = New ListViewTextSort(2, SortOrder.Descending, VariantType.Date)
lvwEvents.ResumeLayout()
Me.lvwEvents.Visible = True
Me.Cursor = Cursors.Default
I figured it had to do with repainting the list, so I set the Visible property to False while it loops. No better. I set the BeginUpdate and EndUpdate accordingly and that doesn't help. I tried to use:
Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long
but I can't get a handle for the control that works with the call (I didn't spend much time on this, I doubt it will help). I also SuspendLayout and ResumeLayout.
The only thing I can think of yet is to create the Listview in code and destroy it after each use. Would this work, and if not, what else? Should I just bite the bullet and write it with some other control
Me.Cursor = Cursors.WaitCursor
'load the list view columns
lvwEvents.Clear()
lvwEvents.SuspendLayout()
lvwEvents.Columns.Add("EventLogID", 0, HorizontalAlignment.Left)
lvwEvents.Columns.Add("Type", 100, HorizontalAlignment.Left)
lvwEvents.Columns.Add("TimeGenerated", 150, HorizontalAlignment.Left)
lvwEvents.Columns.Add("SourceName", 125, HorizontalAlignment.Left)
lvwEvents.Columns.Add("Message", 500, HorizontalAlignment.Left)
lvwEvents.Columns.Add("Category", 70, HorizontalAlignment.Left)
If Not bEmpty Then
drSQL = cCommonData.ExecuteSQL_GetReader(strSQL)
Dim fColor As System.Drawing.Color
Dim Litem As ListViewItem
Dim flag As Boolean = False
Me.lvwEvents.Visible = False
Dim start As System.DateTime = System.DateTime.Now
Dim finish As System.DateTime
Dim i As Integer
lvwEvents.BeginUpdate()
While drSQL.Read
i = i + 1
Litem = New ListViewItem
Litem.Text = (drSQL.Item("EventLogID"
Litem.SubItems.Add(drSQL.Item("Type"
Litem.SubItems.Add(drSQL.Item("TimeGenerated"
Litem.SubItems.Add(drSQL.Item("SourceName"
Litem.SubItems.Add(drSQL.Item("Message"
Litem.SubItems.Add(drSQL.Item("Category"
lvwEvents.Items.Add(Litem)
End While
lvwEvents.EndUpdate()
finish = Date.Now
Dim span As System.TimeSpan = finish.Subtract(start)
Console.WriteLine(EventLogType & "(" & i & "
' Close and Clean up objects
drSQL.Close()
End If
Me.lvwEvents.Tag = Nothing
Me.lvwEvents.ListViewItemSorter = New ListViewTextSort(2, SortOrder.Descending, VariantType.Date)
lvwEvents.ResumeLayout()
Me.lvwEvents.Visible = True
Me.Cursor = Cursors.Default