确定在VB.Net中启动的进程,以便以后可以杀死该进程及其所有子进程 [英] Identify a process started in VB.Net to be able to kill it and all its children later

查看:195
本文介绍了确定在VB.Net中启动的进程,以便以后可以杀死该进程及其所有子进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VB.Net中启动进程时,我希望能够识别它,以便以后在必要时将其及其所有子级杀死.

When starting a process in VB.Net, I would like to be able to identify it to later kill it and all its children if necessary.

我以这种方式启动流程:

I launch my process this way :

Dim mainProcessHandler As New ProcessStartInfo()
Dim mainProcess As Process
mainProcessHandler.FileName = "something_01out18.bat"
mainProcessHandler.Arguments = "-d"
mainProcessHandler.WindowStyle = ProcessWindowStyle.Hidden
mainProcess = Process.Start(mainProcessHandler)

如果我这样做

mainProcess .Kill()

它将关闭所有打开的cmd窗口.但是bat脚本没有启动任何其他进程.

it will close all cmd windows opened. But none of any other process that have been launched by the bat script.

我非常确定操作系统在启动时会给我的进程一个ID,但是我没有成功获取它.如果不是这种情况,我也找不到如何自己给该进程提供ID的方法.

I'm pretty sure that the OS give an ID to my process when I start it but I didn't succeed to get it. If that not the case, I didn't find either how to give a ID myself to the process.

最后,我想列出所有子进程,杀死它们,杀死父进程,但PC上没有其他"cmd"进程运行.

At the end of the day, I would like to list all child processes, kill them, kill the father process but none of the other "cmd" process running on the PC.

有没有办法做这样的事情?

Is there a way to do such things ?

推荐答案

基于作为对找到我自己的.NET进程的所有子进程,我使用了以下代码,这些代码似乎可以杀死知道其PID的进程的所有子进程:

Based on the C# code given as part of an answer to Find all child processes of my own .NET process, I used the following code that seem to work to kill all children of a process knowing its PID :

Sub killChildrenProcessesOf(ByVal parentProcessId As UInt32)
    Dim searcher As New ManagementObjectSearcher(
        "SELECT * " &
        "FROM Win32_Process " &
        "WHERE ParentProcessId=" & parentProcessId)

    Dim Collection As ManagementObjectCollection
    Collection = searcher.Get()

    If (Collection.Count > 0) Then

        consoleDisplay("Killing " & Collection.Count & " processes started by process with Id """ & parentProcessId & """.")
        For Each item In Collection
            Dim childProcessId As Int32
            childProcessId = Convert.ToInt32(item("ProcessId"))
            If Not (childProcessId = Process.GetCurrentProcess().Id) Then

                killChildrenProcessesOf(childProcessId)

                Dim childProcess As Process
                childProcess = Process.GetProcessById(childProcessId)
                consoleDisplay("Killing child process """ & childProcess.ProcessName & """ with Id """ & childProcessId & """.")
                childProcess.Kill()
            End If
        Next
    End If
End Sub

有关信息,请:

Imports System.Management

然后,如果您使用Visual Studio,则需要进入项目菜单,选择添加引用...",在".Net"选项卡下,向下滚动到"System.Management"并选择该行对应于"System.Management",然后单击确定". (如此答案所述)

Then if you use Visual Studio, you'll need to go under the project menu, select "Add Reference...", under the ".Net" tab, scroll down to "System.Management" and select the line corresponding to "System.Management" and click OK. (as stated in this answer)

如果正确调用此Sub,则可以杀死由父进程启动的所有子级.如果需要,可以使用基本的parentProcess.kill杀死父进程.

When correctly called, this Sub is satisfying to kill all children started by a parent process. If need, a basic parentProcess.kill kill the parent process.

这篇关于确定在VB.Net中启动的进程,以便以后可以杀死该进程及其所有子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆