1,105,625 Community Members

How do I differentiate between a pre-existing process and one I started

Member Avatar
renzlo
Junior Poster
143 posts since Oct 2011
Reputation Points: 4 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi, Everyone,

I've coded a program that uses a web browser control and loads a java applet, the process created when the java applet was loaded is java.exe, then on another tab, I've executed again a java applet and created again a java.exe process, now I need to kill the java.exe process for the specific web browser. How can I do that?

The one I've found in google kills all java.exe process, how can I exactly kill the process?

Any inputs will be appreciated. Thank you in advance.

Member Avatar
tinstaafl
Postaholic
2,012 posts since Jun 2010
Reputation Points: 559 [?]
Q&As Helped to Solve: 403 [?]
Skill Endorsements: 36 [?]
 
0
 

If I'm not mistaken, when you kill the java applet the associated java.exe should die with it.

Member Avatar
Reverend Jim
Noli mentula
5,463 posts since Aug 2010
Reputation Points: 770 [?]
Q&As Helped to Solve: 653 [?]
Skill Endorsements: 51 [?]
Moderator
Featured
 
0
 

I don't have access to a system to verify this but two options come to mind.

  1. get a list of all PIDs of processes named java.exe just prior to launching the new instance then use that list to determine the PID of the new process, then use that PID when it is time to kill it.

  2. see if the PID of the new process is returned by the call that spawns the new java.exe and use that to do the kill later on.

Member Avatar
renzlo
Junior Poster
143 posts since Oct 2011
Reputation Points: 4 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 0 [?]
 
0
 

@tinstaafl

Yes, you're right but it has a 10-20 seconds delay, I want to kill it immediately.

@Rev Jim,

Thanks for the reply, actually I've tried the first option but it is not stable, how can I know that it is a new instance of java.exe?

Member Avatar
Reverend Jim
Noli mentula
5,463 posts since Aug 2010
Reputation Points: 770 [?]
Q&As Helped to Solve: 653 [?]
Skill Endorsements: 51 [?]
Moderator
Featured
 
0
 

For a question like this I usually like to play with it myself to verify but I can't do that because my laptop is in the shop, probably until after the weekend. My temp is n old Thinkpad with little RAM, no dev tools and a flky "A" key. If you can wait I cn have a look in a few days. If not, perhaps someone else can chime in.

Member Avatar
renzlo
Junior Poster
143 posts since Oct 2011
Reputation Points: 4 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 0 [?]
 
0
 

ok, I'll wait Jim. Thank you.

Member Avatar
TnTinMN
Practically a Master Poster
640 posts since Jun 2012
Reputation Points: 418 [?]
Q&As Helped to Solve: 152 [?]
Skill Endorsements: 16 [?]
 
2
 

This does not answer the question you posted, but it does what you said you wanted to accomplish; kill the java process currently running under the browser control.

Imports System.Runtime.InteropServices

Public Class Form1

   Private Sub btnKillJava_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKillJava.Click
      KillJavaForBrowser(WebBrowser1)
   End Sub

   Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
      BrowserRefresh(WebBrowser1)
   End Sub

   Private Sub KillJavaForBrowser(ByVal browser As WebBrowser)
      ' get a list of the browser's child windows
      Dim childHandles As List(Of IntPtr) = NativeMethods.GetChildWindows(browser.Handle)

      ' make a list of PID's associated with these child window handles

      Dim childPIDs As New List(Of IntPtr)
      Dim pid As IntPtr

      For Each h As IntPtr In childHandles
         pid = IntPtr.Zero
         NativeMethods.GetWindowThreadProcessId(h, pid)
         childPIDs.Add(pid)
      Next h

      ' loop through the unique child window PID's searching for java.exe

      For Each pid In childPIDs.Select(Function(id As IntPtr) id).Distinct.ToList
         Dim p As Process = Process.GetProcessById(pid.ToInt32)
         If p.MainModule.ModuleName.ToLower = "java.exe" Then
            ' found it, so kill it and stop loop
            p.Kill()
            Exit For
         End If
      Next pid
   End Sub

   Private Sub BrowserRefresh(ByVal browser As WebBrowser)
      browser.Navigate(browser.Url)
   End Sub
End Class


Public Class NativeMethods
   <DllImport("User32.dll")> _
   Private Shared Function EnumChildWindows _
     (ByVal WindowHandle As IntPtr, ByVal Callback As EnumWindowProcess, _
     ByVal lParam As IntPtr) As Boolean
   End Function

   Public Delegate Function EnumWindowProcess(ByVal Handle As IntPtr, ByVal Parameter As IntPtr) As Boolean

   Public Shared Function GetChildWindows(ByVal ParentHandle As IntPtr) As List(Of IntPtr)
      Dim ChildrenList As New List(Of IntPtr)
      Dim ListHandle As GCHandle = GCHandle.Alloc(ChildrenList)
      Try
         EnumChildWindows(ParentHandle, AddressOf EnumWindow, GCHandle.ToIntPtr(ListHandle))
      Finally
         If ListHandle.IsAllocated Then ListHandle.Free()
      End Try
      Return ChildrenList
   End Function

   Private Shared Function EnumWindow(ByVal Handle As IntPtr, ByVal Parameter As IntPtr) As Boolean
      Dim ChildrenList As List(Of IntPtr) = CType(GCHandle.FromIntPtr(Parameter).Target, Global.System.Collections.Generic.List(Of Global.System.IntPtr))
      If ChildrenList Is Nothing Then Throw New Exception("GCHandle Target could not be cast as List(Of IntPtr)")
      ChildrenList.Add(Handle)
      Return True
   End Function

   <DllImport("user32.dll", SetLastError:=True)> _
   Public Shared Function GetWindowThreadProcessId(ByVal hwnd As IntPtr, _
                          ByRef lpdwProcessId As IntPtr) As Int32
   End Function
End Class
Member Avatar
renzlo
Junior Poster
143 posts since Oct 2011
Reputation Points: 4 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 0 [?]
 
0
 

HI, TnTinMN,

I'll try this one.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: