0

下面的编程结构是否可以在 VBScript 中实现。ProgA 将在哪里开始,在执行一些行之后,它将产生两个进程,即 ProgB 和 ProgC。当这些子 .vbs 完成时,父程序 ProgA 将继续执行并将完成任务

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

谢谢,

4

1 回答 1

3

Read about then .Run and .Exec methods of the WshShell object (CreateObject("Wscript.Shell")). Make sure you pay attention to the bWaitOnReturn parameter to .Run and the .Status (and .Exitcode) properties of the WshScriptExec object. This answer contains sample code for .Run and .Exec.

Update:

a.vbs (not production quality code!):

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

.Runs b.vbs:

MsgBox(WScript.ScriptName)
WScript.Quit 22

and .Execs c.vbs:

MsgBox(WScript.ScriptName)
WScript.Quit 33

output:

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

The MsgBoxes will prove that a.vbs waits for b.vbs and c.vbs.

Update II - VBScript's MultiProcessing ((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

and cx.vbs:

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

Don't do this at work without a lot of further effort invested in error handling.

于 2012-12-18T19:58:00.443 回答