kservice Posted August 19, 2004 Posted August 19, 2004 Hi There, I have searched this site for "closing processes" and found some great stuff. I copied and changed some code that I found and have come up with the following function. Public Function PrevInstance() As Boolean Dim processes(), p As Process Try processes = Process.GetProcesses() For Each p In processes If Not (p.MainModule Is Nothing) Then If System.IO.Path.GetFileName(p.MainModule.FileName).ToLower() = "winword.exe" Then p.Kill() Exit For End If End If Next p Catch ex As Exception Throw Finally End Try End Function The problem is that it works great if it finds what it's looking for. If "Word" is not running than I get the following error: Run-time exception thrown: System.ComponentModel.Win32Exception - Unable to enumerate the process modules. I hope it's just something small that I'm missing. Thanks for your time. Quote
Administrators PlausiblyDamp Posted August 19, 2004 Administrators Posted August 19, 2004 you could just change the line that reads throw to something like MessageBox.Show ("Process not found") to stop it crashing out. However if you step through the code in the debugger which line is the one it fails on? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
kservice Posted August 19, 2004 Author Posted August 19, 2004 It fails out on the line: If Not (p.MainModule Is Nothing) Then It's like it doesn't know it's reached the end of the processes or something. Thanks Quote
kservice Posted August 19, 2004 Author Posted August 19, 2004 So I have stepped through it some more and at least can dup the error every time. There are usually 26 processes running when I try this code and I get to process number 23 and it blows on "If Not (p.MainModule Is Nothing) Then". Help? :rolleyes: Quote
Administrators PlausiblyDamp Posted August 19, 2004 Administrators Posted August 19, 2004 What is the value of p at this point - you may find you should be checking for if Not p is nothing Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Kalyana Posted May 9, 2006 Posted May 9, 2006 What is the exact problem. Can you please explain me? Quote
kservice Posted May 9, 2006 Author Posted May 9, 2006 It's been almost two years. I don't remember. :p Sorry! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.