Click here to Skip to main content
15,894,284 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
As stated, my form loads slow while looping through processes. I'm catching all the "access denied(5) exceptions" and I think that's what is slowing things down. I'm even using a "continue for" (which I've never used before) to see if that would speed it up. I know it helps though. Here's my code:
    Try
        strName = Process.GetProcessesByName(p.ProcessName)(0).MainModule.FileName.ToString()
    Catch
        Continue For
    End Try


    exeIcon = Icon.ExtractAssociatedIcon(strName)

    If strName.ToLower <> "idle" Or strName.ToLower <> "system" Then

        imgList.Images.Add(exeIcon)

        blist.Items.Add(imgList.Images(i))
        blist.Items.Add(strName)
        blist.Items.Add(p.Id)
        i += 1

    End If

Next


Does anyone see where I can optimize this code? Is there a faster way to handle the exceptions? Maybe there's a way to block more of the exceptions? Thanks for looking.
Posted
Updated 27-Sep-15 7:04am
v3

Looping through the processes may be slow and as you have done you have to be prepared for exceptions, which again cause delay.

From your code it's not clear what you're looping (the For is missing) but what if you would use GetProcesses[^] to get all the processes before entering the loop and to avoid constantly searching for the processes.

Another(small) enhancement could be the usage of a With[^] block, for example for blist.

Another thing is that since you can't make this lightning fast, why not show a 'splash' screen for the user and tell about the progress. You could have a background worker searching for the processes and the UI would show what happens all the time. This doesn't take away the duration but it gives a better user experience when you see that the program isn't hung.
 
Share this answer
 
v2
Comments
jumper77 27-Sep-15 13:18pm    
Oops, I did leave out the for part (sorry).


For Each p As Process In Process.GetProcesse

I've already thought about the splash screen and if I couldn't make it any faster, I would go that route.

I'm a bit of a noob, but I will check out the link to see if it helps. Thanks a bunch for your help.
Wendelius 27-Sep-15 13:55pm    
Hope it helps :)
jumper77 27-Sep-15 14:17pm    
will let you know something soon. Have to run some errands first.
@Mika Well, I changed my code just a bit and for some reason, it's faster now. So far, it doesn't make sense to me. Maybe it will come to me later. Here's the code after I changed it. The ListView windows only takes 2 seconds to load and I can live with that.

blist.Clear() 'clear listbox. In case listbox window has been closed then reopened

imgList.ImageSize = New Size(30, 30)

For Each p As Process In Process.GetProcesses

    Try

        strName = Process.GetProcessesByName(p.ProcessName)(0).MainModule.FileName.ToString()

        If strName.ToLower <> "idle" Or strName.ToLower <> "system" Then

            exeIcon = Icon.ExtractAssociatedIcon(strName)

            imgList.Images.Add(exeIcon)

            blist.Items.Add(imgList.Images(i))
            blist.Items.Add(strName)
            blist.Items.Add(p.Id)
            i += 1
        End If

        'MsgBox(p.Id)
    Catch
        Continue For
    End Try
Next


I moved all my code inside of the "try" block. It works now. Maybe someone will tell me how :) Thanks for the help Mika
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900