VB6:如何等待命令执行

时间:2016-07-11 02:05:59

标签: shell vb6 exec

我下面的代码运行没有错误。

但是我需要等到该进程在此行被杀死WshShell.Exec "TASKKILL /F /IM " & EngineRun.ProcessID,然后才显示msgbox已终止该进程。

Option Explicit

Dim WshShell As Object
Dim EngineRun As Object

Sub main()

Set WshShell = CreateObject("WScript.Shell")
Set EngineRun = WshShell.Exec("notepad.exe")

MsgBox EngineRun.ProcessID

WshShell.Exec "TASKKILL /F /IM " & EngineRun.ProcessID

MsgBox EngineRun.ProcessID & (" terminated")

End Sub

感谢您的帮助

2 个答案:

答案 0 :(得分:2)

您可以使用Run方法吗?见MSDN。它支持bWaitOnReturn参数:

object.Run(strCommand, [intWindowStyle], [bWaitOnReturn]) 

所以你的代码看起来像是:

WshShell.Run "TASKKILL /F /IM " & EngineRun.ProcessID,4 , True

答案 1 :(得分:1)

Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")

Set colItems = objWMIService.ExecQuery("Select * From Win32_Process")

For Each objItem in colItems
    'msgbox objItem.ProcessID & " " & objItem.CommandLine
    If objItem.name = "Calculator.exe" then objItem.terminate
Next

妥善处理。