Very slow code...

?

?BOT

Why is the following code so slow? I have a listView that must refresh it's
processes data every 3 seconds. However, this sometimes goes beyond 5
seconds and crashes the application. I have tries multi-threading, also
ListView.BeginUpdate + EndUpdate(), but the code is sooo slow. Try it for
yourself:

Public Sub UpdateData()
On Error Resume Next
Me.ListView1.Clear()
For Each process As System.Diagnostics.Process In
Me.Process1.GetProcesses()
Dim ListItem As New ListViewItem(process.ProcessName)
With ListItem
.SubItems.Add(process.MainModule.FileName)
.SubItems.Add(process.TotalProcessorTime.ToString())
.SubItems.Add(process.PriorityClass.ToString())
.SubItems.Add(process.PagedMemorySize)
.SubItems.Add(process.NonpagedSystemMemorySize)
.SubItems.Add(process.Modules.Count)
.SubItems.Add(process.MainModule.FileVersionInfo.CompanyName)
End With
Me.ListView1.Items.Add(ListItem)
Next
End Sub


Any help is greatly appreciated.
 
C

cody

Add all items to an Array and then use ListViewItem.Items.AddRange() this is
much faster.
 
C

Cor Ligthert

Bot,

In my opinion does as Cody told you help you, however be aware that in a for
each loop the collection that it uses has to exist as long as the for each
loop is done.

The change (because you are as well doing screen operations) is in my
opinion very high that a process is stopped and even more with more
processors or hyperthreading. I think that I would protect this routine in a
try and catch block.

However not tested a guess.

Cor
 
C

cody

Remember that you must never access a WinForms control you always have to
marshall the call using myControl.BeginInvoke().
 
?

?BOT NET>

Thanks very much cody. I have done as you said, and the refresh rate is now
acceptable. However, when the list view refreshes, the application consumes
up to 95 percent of CPU resources. This is not acceptable at all. How should
I avoid my application to be quiet like Windows task manager which goes only
upto 4 percent in each refresh session.


Public Overridable Sub UpdateDate()
On Error Resume Next
Dim ListItemsArr(Me.ProcessMain.GetProcesses.Length) As ListViewItem
Dim i As Integer = 0
For Each process As System.Diagnostics.Process In
Me.ProcessMain.GetProcesses
Dim ListItem As New ListViewItem(process.ProcessName)
With ListItem
.SubItems.Add(process.MainModule.FileName)
.SubItems.Add(process.PriorityClass.ToString)
.SubItems.Add(process.TotalProcessorTime.ToString)
.SubItems.Add(process.PagedMemorySize)
.SubItems.Add(process.NonpagedSystemMemorySize)
.SubItems.Add(process.Modules.Count)
.SubItems.Add(process.MainModule.FileVersionInfo.CompanyName)
End With
ListItemsArr(i) = ListItem
i += 1
Next

Dim selIndex As Integer
If ListView1.Items.Count > 0 Then
If Me.ListView1.SelectedItems.Count > 0 Then
selIndex = Me.ListView1.SelectedIndices(0)
End If
Me.ListView1.Items.Clear()
End If

ListView1.BeginUpdate()
Me.ListView1.Items.AddRange(ListItemsArr) < --- here is what you
told me...
ListView1.ListViewItemSorter = New
MyListViewSorter(Me._ColumnClicked)
ListView1.EndUpdate()
Me.ListView1.Items(selIndex).Selected = True

End Sub
 

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