首页 > 解决方案 > 当单独的进程关闭时关闭进程

问题描述

因此,我使用任务调度程序在启动特定程序 (c:\Program.exe) 后运行特定文件 (c:\Newfile.exe)。

我想知道当“Program.exe”关闭时如何关闭“Newfile.exe”。我知道这不能用任务调度程序来完成,有没有人知道实现这种场景结果的方法?

亲切的问候

尝试使用任务调度程序

标签: batch-fileprocesstaskscheduler

解决方案


该程序运行等待程序退出,检查它是否是 Notepad.exe,如果是,则终止 MyProgram.exe。Exit Do在终止程序后使用也退出脚本。

Set WshShell = WScript.CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2") 
Set objEvents = objWMIService.ExecNotificationQuery _
    ("SELECT * FROM Win32_ProcessStopTrace")

Do
    Set objReceivedEvent = objEvents.NextEvent
    If lcase(objReceivedEvent.ProcessName) = lcase("Notepad.exe") then 
        Msgbox "Process exited with exit code " & objReceivedEvent.ExitStatus
        Set colItems = objWMIService.ExecQuery("Select * From Win32_Process where ProcessName=MyProgram.exe")
        For Each itm in ColItems
             itm.Terminate
        Next
    End If
Loop

推荐阅读