Resolved How to avoid exceptions while looping through processes

jumper77

Member
Joined
Jan 23, 2019
Messages
15
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...
VB.NET:
 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:
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 :)
 
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.
 
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?
 
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
 
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.
 
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 :)
 

Latest posts

Back
Top