473,378 Members | 1,369 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 2350
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: DJJ | last post by:
I am using the MySQL ODBC 3.51 driver to link three relatively small MySQL tables to a Microsoft Access 2003 database. I am finding that the data from the MySQL tables takes a hell of a long time...
2
by: Steve K | last post by:
Hi, I'm on a Mac, OSX version 10.3.5, 1.25GB SDRAM, 400 MHz PowerPC G4 I have DreamweaverMX 2004, version 7.0.1. It is painfully slow!! It's very slow working in design view, and almost...
6
by: B B | last post by:
Okay, here is what's happening: I have a reasonably fast laptop (1.4 GHz Mobile M, so comparable to 2.5GHz P4) doing .net development. Running Windows XP pro, SP2 IIS is installed and running...
6
by: Michael | last post by:
Hi, I'm new to VB.Net but have noticed a few things about the dev interface that is really starting to get annoying. First of all, while trying to work with a form and the code module for the form...
50
by: diffuser78 | last post by:
I have just started to learn python. Some said that its slow. Can somebody pin point the issue. Thans
5
by: PH | last post by:
Hi guys; I got a single processor computer, running an application that launches 2 threads. Each of these threads listens for incoming connections in a specific port, so there is a Loop ....
9
by: dan | last post by:
within a loop i am building a sql insert statement to run against my (programatically created) mdb. it works but it seems unreasonably SLOW! Sorry, dont have the code here but the jist is very...
4
by: rubyhcurry | last post by:
Hello, I have a small application which acts like a wizard with 5 steps. I use a tab control, and 'back' and 'next' buttons to switch between the 5 tabs (1 tab page for each step). The...
3
by: Ryan Liu | last post by:
Hi, I spend most time works on Windows form application and VS 2008 runs at a acceptable speed. While I edit aspx file, especially change to Designer View, it seems very slow. Some time...
0
by: Kerem Gümrükcü | last post by:
Hi, i use the code from this code sample on MSDN: for printing a 5 and sometimes 70 page text-only data: http://msdn.microsoft.com/en-us/library/ms404294.aspx The point is, that this is...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.