【问题标题】:Does .VBS support Hold-Wait program paradigm?.VBS 是否支持 Hold-Wait 程序范例?
【发布时间】:2012-12-19 05:43:12
【问题描述】:

下面的编程结构是否可以在 VBScript 中实现。ProgA 将在哪里启动,经过几行执行后,它将产生两个进程,即 ProgB 和 ProgC。当这些子 .vbs 完成时,父程序 ProgA 将恢复它的执行并将完成它的任务

                                    ProgA.VBS
                                        |
                 -------------------------------------------------
                 |                                               |
            ProgB.VBS                                        ProgC.VBS

谢谢,

【问题讨论】:

    标签: vbscript


    【解决方案1】:

    然后阅读 WshShell 对象 (CreateObject("Wscript.Shell")) 的 .Run.Exec 方法。请务必注意 .Run 的 bWaitOnReturn 参数和 WshScriptExec 对象的 .Status(和 .Exitcode)属性。 This answer 包含 .Run 和 .Exec 的示例代码。

    更新:

    a.vbs(不是生产质量代码!):

    Option Explicit
    
    Const WshFinished = 1
    
    Dim goWSH : Set goWSH = CreateObject("WScript.Shell")
    
    Dim sCmd, nRet, oExec
    
    sCmd = "cscript .\b.vbs"
    WScript.Echo "will .Run", sCmd
    nRet = goWSH.Run(sCmd, , True)
    WScript.Echo sCmd, "returned", nRet
    
    sCmd = "cscript .\c.vbs"
    WScript.Echo "will .Exec", sCmd
    Set oExec = goWSH.Exec(sCmd)
    Do Until oExec.Status = WshFinished : WScript.Sleep 100 : Loop
    WScript.Echo sCmd, "returned", oExec.ExitCode
    
    WScript.Echo "done with both scripts"
    WScript.Quit 0
    

    .运行 b.vbs:

    MsgBox(WScript.ScriptName)
    WScript.Quit 22
    

    和.Execs c.vbs:

    MsgBox(WScript.ScriptName)
    WScript.Quit 33
    

    输出:

    cscript a.vbs
    will .Run cscript .\b.vbs
    cscript .\b.vbs returned 22
    will .Exec cscript .\c.vbs
    cscript .\c.vbs returned 33
    done with both scripts
    

    MsgBoxes 将证明 a.vbs 等待 b.vbs 和 c.vbs。

    更新 II - VBScript 的多处理((c) @DanielCook):

    ax.vbs:

    Option Explicit
    
    Const WshFinished = 1
    
    Dim goWSH : Set goWSH = CreateObject("WScript.Shell")
    
    ' Each cmd holds the command line and (a slot for) the WshScriptExec
    Dim aCmds : aCmds = Array( _
        Array("cscript .\bx.vbs", Empty) _
      , Array("cscript .\cx.vbs", Empty) _
    )
    Dim nCmd, aCmd
    For nCmd = 0 To UBound(aCmds)
        ' put the WshScriptExec into the (sub) array
        Set aCmds(nCmd)(1) = goWSH.Exec(aCmds(nCmd)(0))
    Next
    Dim bAgain
    Do
        WScript.Sleep 100
        bAgain = False ' assume done (not again!)
        For Each aCmd In aCmds
            ' running process will Or True into bAgain
            bAgain = bAgain Or (aCmd(1).Status <> WshFinished)
        Next
    Loop While bAgain
    For Each aCmd In aCmds
        WScript.Echo aCmd(0), "returned", aCmd(1).ExitCode
    Next
    
    WScript.Echo "done with both scripts"
    WScript.Quit 0
    

    .Execs bx.vbs

    Do
      If vbYes = MsgBox("Tired of this rigmarole?", vbYesNo, WScript.ScriptName) Then Exit Do
      WScript.Sleep 300
    Loop
    WScript.Quit 22
    

    和 cx.vbs:

    Do
      If vbYes = MsgBox("Tired of this rigmarole?", vbYesNo, WScript.ScriptName) Then Exit Do
      WScript.Sleep 500
    Loop
    WScript.Quit 33
    

    如果没有在错误处理上投入大量精力,请不要在工作中这样做。

    【讨论】:

    • 我可以有简单的Demo代码,它会告诉我们prog A是如何从中产生另外两个进程的,然后等待这两个完成。当这两个完成时,父级是如何通信的并恢复它的任务——一个完整的程序代码作为演示,我可以吗?
    • 我已经给了+1,但是如果OP想要“同时”实际运行b和c。他们只需要对两个进程都使用 Exec 方法,并检查循环中的状态。 (仅针对 OP 说明这一点。)很好的答案。
    • 令人兴奋的@Ekkehard.Horner,我真的很想为我的两个 .vbs 代码实现这种技术,哪种方式可以真正开始?如果我用我的两个实现这个范例,我需要更多的关心吗? :-)
    • 你们也可以投票支持我的帖子吗? :-) 我认为这对其他人的帮助会更大:-)
    • @Ekkehard.Horner 我可以在帖子中提供一些简单的逻辑吗? stackoverflow.com/questions/13947199/… ..请
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多