代码之家  ›  专栏  ›  技术社区  ›  Icode4food

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

  •  2
  • Icode4food  · 技术社区  · 15 年前

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

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

    3 回复  |  直到 14 年前
        1
  •  3
  •   David-W-Fenton    15 年前

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

    API: Shell and Wait

        2
  •  2
  •   PowerUser    15 年前

    shell tasklist (description at http://technet.microsoft.com/en-us/library/bb491010.aspx )是手动执行的好方法。不过,我不确定如何通过自动化与PID列表进行交互。这有点困难。

    提示:如果您只想终止进程,请使用 shell“taskkill/f/im”“excel.exe”“” 来终止MS Excel的所有打开实例。或 shell“taskkill/f/im”“ms access.exe”“” to kill all open instances of ms access(including the access file that hosts your vba script,fally).这是我结束标准问题错误处理程序的方法。

    < H3>编辑< /H3>

    当你运行它时,你应该像这样:

    )是手动操作的好方法。不过,我不确定如何通过自动化与PID列表进行交互。这有点困难。

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

    编辑

    当你运行它时,你应该像这样: alt text

        3
  •  0
  •   Curtis Inderwiesche    14 年前

    我使用以下内容作为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
    

    这对我的项目很好;但是,它需要Windows管理工具。

    推荐文章