kill a specific process

mberube

Member
Joined
Jun 17, 2005
Messages
10
Location
Quebec, Quebec, Canada
Programming Experience
1-3
Hi peeps, I'd like to know if it's possible to get the process ID of a specific application that i started in vb.net. Because my app is opening Excel and another application, and If the user decide to cancel, I need to kill both those processus. I know how I can kill Excel processus, but I want to kill the specific one created bny my app, not every Excel opened.
 
Ok, you can do that bu using GetProcess method of the System.Diagnostic namespace

VB.NET:
'Declare an array of processes
Dim myProcesses() as Process

'single process variable
Dim myProcess As Process

'Get the list of processes
myProcesses = Process.GetProcesses()

'Iterate through the process array 
For Each myProcess in myProcesses
Listview1.Items.add(myProcess.ProcessName)
Next

now you can kill each process particulary just add some code to the listview like

'rough scenario
for each lvitem in listview1.selecteditems ...process.Kill()

Cheers ;)
 
Ok I know that part, but this is not exactly what i want. 1st, in your listview, there would be two processes with the same name. Wich one would i choose that is specificly the one I created, second, I don't want anything that would be called from a user.

So with that process vector, i would need something like this

for each process in myProcesses
if process is myapplication then
process.kill
exit for
end if
next
 
vincent Kasyoki

Use this
system.diagnostics.process.getprocesses(specify all processes).kill()

or
system.diagnostics.process.getcurrentprocess.kill()

Happy killing.
 
Being new VB myself, I wouldn't mind finding out how to do this myself.

I believe his question would be clearer if worded like this: "How do I differentiate between a pre-existing process and one I started myself?" Aka: if processThis is already running and I start another processThis, how do I tell which one I started?
 
Hopefully this will give you a start in the right direction.

You'll need to import the System.Management namespace.

VB.NET:
        Dim query As SelectQuery = New SelectQuery("Win32_Process")
        Dim mgmtSearcher As ManagementObjectSearcher = New ManagementObjectSearcher(query)

        For Each p As ManagementObject In mgmtSearcher.Get()
            Dim s(1) As String
            p.InvokeMethod("GetOwner", DirectCast(s, Object()))

            MessageBox.Show("Name: " & p("Name").ToString() & Environment.NewLine & _
                "Parent Process ID: " & p("ParentProcessId").ToString() & Environment.NewLine & _
                "Process ID: " & p("ProcessId").ToString() & Environment.NewLine & _
                "Handle: " & p("Handle").ToString() & Environment.NewLine & _
                "CS Name: " & p("CSName").ToString() & Environment.NewLine & _
                "Session ID: " & p("SessionId").ToString() & Environment.NewLine & _
                "User: " & s(1) & "\" & s(0))

            If p("Name").ToString() = "EXCEL.EXE" AndAlso s(0).ToString() = "MattP" Then
                p.InvokeMethod("Terminate", Nothing)
            End If
        Next

You can also narrow down the query to only return results with a specified name.

VB.NET:
SELECT * FROM Win32_Process WHERE Name = 'EXCEL.EXE'
 
Last edited:
Solved

I would like to start by thanking MattP for his response. It started me to thinking down paths I had not explored before. In addition, even though it doesn't really solve the issue, I can already think of several apps I will be writing in the next few months that this code will make much easier. Thanks!!:)

Apparently, I was making a mountain out of a molehill. All we really needed was the Process class.

VB.NET:
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ggProcess As New Process

        ggProcess = Process.Start("EXCEL.EXE")

        ggProcess.Kill()

    End Sub

This will open a blank Excel file and then terminate JUST the Excel file it started.:D
 
I would like to start by thanking MattP for his response.

You're welcome. Glad you'll be able to use it as a starting point for future learning.

Apparently, I was making a mountain out of a molehill. All we really needed was the Process class.

Heh. Well I assumed you saw those responses earlier in the thread. :)
 
Back
Top