【问题标题】:VBScript wait functionVBScript 等待函数
【发布时间】:2015-08-09 20:22:02
【问题描述】:

我正在尝试将此函数转换为等待 notepad.exe 为通用等待函数(可以等待任何进程)。我不能在不破坏语法的情况下在 sQuery 中添加变量,有什么办法可以绕过这个问题吗?

Function wait()
Set svc=getobject("winmgmts:root\cimv2")
sQuery="select * from win32_process where name='notepad.exe'"
Set cproc=svc.execquery(sQuery)
iniproc=cproc.count
Do While iniproc = 1
    wscript.sleep 5000
    set svc=getobject("winmgmts:root\cimv2")
    sQuery="select * from win32_process where name='notepad.exe'"
    set cproc=svc.execquery(sQuery)
    iniproc=cproc.count
Loop    
Set cproc=nothing
Set svc=Nothing
End Function

Set fso = WScript.CreateObject("Scripting.Filesystemobject")
Set wscr = CreateObject("Wscript.shell")
wscr.Run "notepad.exe"

wait()

MsgBox "done!"

我要疯了!!

【问题讨论】:

    标签: vbscript


    【解决方案1】:

    您用来启动记事本的WScript.Shell 对象的Run 方法具有等待进程完成的内置机制。您的脚本中根本不需要基于 WMI 的 wait() 函数。

    wscr.Run "notepad.exe", 1, True
    

    行尾的True 值表明Run() 应该等待进程完成。

    但要回答您的具体问题,请向wait 函数添加一个参数。我称它为ProcessName。然后使用这个变量代替notepad.exe。当您调用wait() 时,您将进程的名称指定为参数。

    Function wait(ProcessName)
    Set svc=getobject("winmgmts:root\cimv2")
    sQuery="select * from win32_process where name='" & ProcessName & "'"
    Set cproc=svc.execquery(sQuery)
    iniproc=cproc.count
    Do While iniproc = 1
        wscript.sleep 5000
        set svc=getobject("winmgmts:root\cimv2")
        sQuery="select * from win32_process where name='" & ProcessName & "'"
        set cproc=svc.execquery(sQuery)
        iniproc=cproc.count
    Loop    
    Set cproc=nothing
    Set svc=Nothing
    End Function
    
    Set fso = WScript.CreateObject("Scripting.Filesystemobject")
    Set wscr = CreateObject("Wscript.shell")
    wscr.Run "notepad.exe"
    
    wait("notepad.exe")
    
    MsgBox "done!"
    

    【讨论】:

      【解决方案2】:

      Patrick S.回答了但是,Do While 循环中有两条超丰富的行和两条不必要的行:

      Function wait(sPName)
      Set svc=getobject("winmgmts:root\cimv2")
      sQuery="select * from win32_process where name='" & sPName & "'"
      Set cproc=svc.execquery(sQuery)
      iniproc=cproc.count
      Do While iniproc > 0
          wscript.sleep 5000
          'superabundant' set svc=getobject("winmgmts:root\cimv2")
          'superabundant' sQuery="select * from win32_process where name='notepad.exe'"
          set cproc=svc.execquery(sQuery)
          iniproc=cproc.count
      Loop    
      'unnecessary' Set cproc=nothing
      'unnecessary' Set svc=Nothing
      End Function
      
      Set fso = WScript.CreateObject("Scripting.Filesystemobject")
      Set wscr = CreateObject("Wscript.shell")
      
      sProcessName="calc.exe"
      wscr.Run sProcessName
      wait( sProcessName)
      MsgBox "done " & sProcessName
      
      wscr.Run "notepad.exe"
      wait( "notepad.exe")
      MsgBox "done notepad.exe"
      

      【讨论】:

        【解决方案3】:
        Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2") 
        Set objEvents = objWMIService.ExecNotificationQuery ("SELECT * FROM Win32_ProcessTrace")
        Do
            Set objReceivedEvent = objEvents.NextEvent
            If objReceivedEvent.ProcessName = "svchost.exe" then msgbox objReceivedEvent.ProcessName
        '   msgbox objReceivedEvent.ProcessName
        Loop
        

        Win32_ProcessTrace(开始和停止)、Win32_ProcessTraceStartWin32_ProcessTraceStop。脚本停止并等待事件发生。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-02-06
          • 2019-12-12
          • 2023-03-13
          • 2021-09-23
          • 1970-01-01
          • 2017-12-26
          • 1970-01-01
          相关资源
          最近更新 更多