469,271 Members | 1,729 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

Very slow code...

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.C ompanyName)
End With
Me.ListView1.Items.Add(ListItem)
Next
End Sub
Any help is greatly appreciated.


Nov 22 '05 #1
4 2226
Add all items to an Array and then use ListViewItem.Items.AddRange() this is
much faster.
"?BOT NET>" <BOTTED@ERTYU> schrieb im Newsbeitrag
news:%2****************@TK2MSFTNGP12.phx.gbl...
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.C ompanyName)
End With
Me.ListView1.Items.Add(ListItem)
Next
End Sub
Any help is greatly appreciated.

Nov 22 '05 #2
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
Nov 22 '05 #3
Remember that you must never access a WinForms control you always have to
marshall the call using myControl.BeginInvoke().

"?BOT NET>" <BOTTED@ERTYU> schrieb im Newsbeitrag
news:%2****************@TK2MSFTNGP12.phx.gbl...
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.C ompanyName)
End With
Me.ListView1.Items.Add(ListItem)
Next
End Sub
Any help is greatly appreciated.

Nov 22 '05 #4
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.C ompanyName)
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

"cody" <de********@gmx.de> wrote in message
news:Oe*************@tk2msftngp13.phx.gbl...
Add all items to an Array and then use ListViewItem.Items.AddRange() this
is much faster.
"?BOT NET>" <BOTTED@ERTYU> schrieb im Newsbeitrag
news:%2****************@TK2MSFTNGP12.phx.gbl...
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.C ompanyName)
End With
Me.ListView1.Items.Add(ListItem)
Next
End Sub
Any help is greatly appreciated.



Nov 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by DJJ | last post: by
2 posts views Thread by Steve K | last post: by
6 posts views Thread by Michael | last post: by
50 posts views Thread by diffuser78 | last post: by
5 posts views Thread by PH | last post: by
4 posts views Thread by rubyhcurry | last post: by
3 posts views Thread by Ryan Liu | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.