Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

Posted

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:

  • 1 year later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...