【问题标题】:Close process when a separate process closes当单独的进程关闭时关闭进程
【发布时间】:2019-03-26 23:20:09
【问题描述】:

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

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

亲切的问候

尝试使用任务调度程序

【问题讨论】:

  • 你编程program.exe了吗?如果是这样,请使用作业对象。
  • @Noodles 这些只是示例,真正的文件被埋在它们自己单独的目录中。我使用这些示例来简化线程,以便人们更容易理解。 “Program.exe”是我PC上的软件,我没有编码。 “Newfile.exe”是一个 AHK 脚本。
  • 您可以编写一个启动程序,启动两个程序并在另一个程序终止时终止一个程序。

标签: batch-file process task scheduler


【解决方案1】:

该程序运行等待程序退出,检查它是否是 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

【讨论】:

    猜你喜欢
    • 2011-12-30
    • 2012-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-27
    • 1970-01-01
    • 2014-06-06
    相关资源
    最近更新 更多