vba 检查特定进程任务 ID 是否仍在运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3980918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 12:13:24  来源:igfitidea点击:

vba check if specific process task id is still running

ms-accessvbashell

提问by Icode4food

I am using the Shell()function to execute an external application in MS Access. Shell()returns a task ID of the specific process that was started.

我正在使用该Shell()函数在 MS Access 中执行外部应用程序。Shell()返回已启动的特定进程的任务 ID。

I need a way to check if that process is still running or if it has closed. I'm sure that I will need to do some sort of windows API call but I can't seem to find anything just now.

我需要一种方法来检查该进程是否仍在运行或是否已关闭。我确定我需要进行某种 Windows API 调用,但我现在似乎找不到任何东西。

采纳答案by David-W-Fenton

This is an Access FAQ, and it was answered years and years ago on the FAQ site for comp.databases.ms-access, http://mvps.org/access/.

这是一个 Access 常见问题解答,多年前在 comp.databases.ms-access 的常见问题解答站点http://mvps.org/access/上得到了解答。

API: Shell and Wait

API:外壳和等待

回答by PowerUser

Shell Tasklist(Description at http://technet.microsoft.com/en-us/library/bb491010.aspx) is a good way to do it manually. I'm not sure how you could interact with the list of PIDs by automation, though. That's a bit more difficult.

Shell Tasklisthttp://technet.microsoft.com/en-us/library/bb491010.aspx 上的说明)是手动执行此操作的好方法。不过,我不确定您如何通过自动化与 PID 列表进行交互。这有点困难。

Tip: If you just want to kill a process, use Shell "TaskKill /F /IM ""excel.exe"""to kill all open instances of MS Excel. Or Shell "TaskKill /F /IM ""msaccess.exe"""to kill all open instances of MS Access (including the Access file that hosts your VBA script, unfortunately). This is how I end my standard issue Error Handler.

提示:如果您只想终止进程,请使用Shell "TaskKill /F /IM ""excel.exe"""终止所有打开的 MS Excel 实例。或者Shell "TaskKill /F /IM ""msaccess.exe"""杀死所有打开的 MS Access 实例(不幸的是,包括承载 VBA 脚本的 Access 文件)。这就是我如何结束我的标准问题错误处理程序。

Edit

编辑

When you run it, you should something like: alt text

当你运行它时,你应该是这样的: 替代文字

回答by Curtis Inderwiesche

I used the following as part of the WMI:

我使用以下作为 WMI 的一部分:

Public Function WaitForProcess(ProcessName As String, Optional ProcessID As Boolean = False)

    ' if you dont mind including the reference for Microsoft WMI Scripting V.1.2 Library, uncomment below
    ' Otherwise, it should work fine without the reference, just no intelasense

    UpdateStatus "Starting WaitForProcess function to wait for process: " & ProcessName

    Dim strComputer As String
    Dim colProcesses
    Dim objWMIService

    UpdateStatus "Use this computer to see processes on"
    strComputer = "."

    UpdateStatus "Impersonate this computer"
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\" & strComputer & "\root\cimv2")

    UpdateStatus "Get all running processes with a " & IIf(ProcessID = False, "Name", "ProcessID") & " of: " & ProcessName
    Set colProcesses = objWMIService.ExecQuery("SELECT * " & _
                                               "FROM Win32_Process  " & _
                                               "WHERE " & IIf(ProcessID = False, "Name", "ProcessID") & " = '" & ProcessName & "'")

    UpdateStatus "Waiting untill there are no more instances of this process shown in the process list"
    Do
        DoEvents
        ' Update the collection of processes returned

        Set colProcesses = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE " & IIf(ProcessID = False, "Name", "ProcessID") & " = '" & ProcessName & "'")


    Loop Until colProcesses.Count = 0

    UpdateStatus "The " & IIf(ProcessID = False, "Process Name", "Process ID") & ": " & ProcessName & " concluded successfully. WaitForProcess Completed"

End Function

This works well for my project; however, it does require Windows Management Instrumentation.

这对我的项目很有效;但是,它确实需要 Windows Management Instrumentation。