|
-
Feb 24th, 2010, 07:32 PM
#1
Freeing up system resources
I'm getting a collection of processes, adding them to a ListView, and deleting them from it when needed. All of this is done in a timer.
The problem, is that after a while, the app freezes. I think this is because I'm not disposing any variables.
Here's my code:
VB.NET Code:
Private _processes As New List(Of String)()
Dim killedProcs As New List(Of String)(_processes)
Dim processes As Process() = System.Diagnostics.Process.GetProcesses()
For Each proc As Process In processes
Dim procId As String = proc.Id.ToString()
If Not _processes.Contains(procId) Then
_processes.Add(procId)
Dim lvi As New ListViewItem(proc.ProcessName)
If strImageKey <> String.Empty Then
lvi.ImageKey = strImageKey
End If
lvi.Name = procId
Try
'Add Process ID to lvwProcess
lvi.SubItems.Add(proc.Id.ToString())
Catch ex As Exception
End Try
Try
'Add CPU usage to lvwProcess
lvi.SubItems.Add("")
Catch ex As Exception
End Try
Try
'Add memory usage to lvwProcess
lvi.SubItems.Add(proc.WorkingSet64 / 1024 & " K")
Catch ex As Exception
End Try
Try
'Add the companies name to the lvwProcess, also known as the Vendor Name
lvi.SubItems.Add(proc.MainModule.FileVersionInfo.CompanyName)
Catch ex As Exception
End Try
Try
lvi.SubItems.Add(proc.MainModule.FileVersionInfo.FileDescription)
Catch ex As Exception
End Try
Try
'Adds the image path to the lvwProcess
lvi.SubItems.Add(proc.MainModule.FileName)
Catch ex As Exception
End Try
lvwProcess.Items.Add(lvi)
Else
killedProcs.Remove(procId)
End If
Next
'remove the killed procs in the list view
For Each procId As String In killedProcs
lvwProcess.Items.RemoveByKey(procId)
Next
I tried disposing of my process list, but the IDE wouldn't let me. That's the only thing I thought would need disposing since it is what hold all of the processes.
Any ideas?
Thanks
CodeBank contributions: Process Manager, Temp File Cleaner
 Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|