• Hello and welcome to our new forums. We upgraded our forum sites to a more robust and modern system which we hope you will enjoy. Be sure to check out your profile by clicking the button on the top right and configure your preferences, signature, time zone, avatar, etc. as you wish. If you need help with using this new forum'ware try the help link on the bottom right.

    Click here to review your account now.

Resolved How to avoid exceptions while looping through processes

jumper77

Member
Joined
Jan 23, 2019
Messages
13
Location
Jackson Tennessee
Programming Experience
Beginner
Hi everyone... I have a routine that loops through processes and I'm using a try and catch block to catch exceptions. What I would like to find out is if there is a way to avoid the exception completely. I'm seeing a high cost in execution time because of all the exceptions. Here's the place in the code where the exception occurs...
Code:
 Dim file As String = p.MainModule.FileName
I'm pretty sure that most people have seen this firsthand. In my case I think all of the errors are "Access Denied". Is there any kind of check that I can do to avoid this altogether?

Thanks in advance!
 
Last edited:

jumper77

Member
Joined
Jan 23, 2019
Messages
13
Location
Jackson Tennessee
Programming Experience
Beginner
I just found an answer that is good enough. I check to see if Process.SessionID is 0 before getting the filename now. The form is loading quick now. I'm a bit brain dead at the moment so I'm not positive this is the workaround, but for now I'm happy and I'm going to sleep :)
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,254
Location
Norway
Programming Experience
10+
MainModule of most processes with SessionID=0 can be accessed if you run the app elevated as admin.

I think the limitation is Process.GetCurrentProcess.SessionId for regular user process, but even for those some access is denied or could throw other exceptions.
 

jumper77

Member
Joined
Jan 23, 2019
Messages
13
Location
Jackson Tennessee
Programming Experience
Beginner
Hi John. I have my manifest file set to "HighestAvailable", but just found out something I didn't know about... I took the check for Process.SessionId out and launched the release version (outside of Visual Studio) and everything worked fine. Until now I've been running it in the debugger, so of course now I have a new question for you. Why is it different when running the program inside Visual Studio?
 

jumper77

Member
Joined
Jan 23, 2019
Messages
13
Location
Jackson Tennessee
Programming Experience
Beginner
Sorry for the extra reply, but just hold off on commenting because I'm getting really odd behavior from the software right now. I have to fix that before anything will make sense. My apology
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,254
Location
Norway
Programming Experience
10+
It could depend on VS version, for older versions there was a hosting process that could prevent this check (you could start VS as admin yourself to debug elevated).
I'm using 2017 and it ask to restart VS as admin (elevation) if I include that manifest and started VS normally.
 

jumper77

Member
Joined
Jan 23, 2019
Messages
13
Location
Jackson Tennessee
Programming Experience
Beginner
Thank you. I'm using 2017 also and it's in debug elevated as well (debug elevated). But I have everything working right now. My confusion was due to a "stupid" mistake I had made (hangs head in shame...)
Thank you for the help my friend. I will make an effort in the future not to ask questions so quickly :)
 
Top Bottom