2012-12-18 42 views
0

可以在VBScript中使用下面的編程結構。在ProgA啓動的時候,在執行一些代碼之後,它會生成兩個procsses,分別爲ProgB和ProgC。當這些子程序.vbs完成時,父程序ProgA將恢復其執行,將完成其TAKS.VBS支持Hold-Wait程序範例嗎?

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

感謝,

回答

3

閱讀然後.Run.Exec WshShell對象(CreateObject("Wscript.Shell"))的方法。請確保您注意WshScriptExec對象的.Run和。狀態(和.Exitcode)屬性的bWaitOnReturn參數。 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 

.Runs 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 

如果不進行大量的錯誤處理工作,不要在工作中這樣做。

+0

我可以有簡單的演示代碼,它會告訴我們prog A如何從它誕生了另外兩個過程,然後等待這兩個過程完成。當這兩個過程完成後,父母如何獲得溝通並恢復其任務 - - 作爲Demo的完整程序代碼,我可以嗎? –

+2

我已經給了+1,但是如果OP想要在「同一時間」實際運行b和c。他們只需在兩個進程中使用Exec方法,並在循環中檢查狀態。 (只對OP說明這一點)很好的答案。 –

+0

讓人頭疼@Ekkehard.Horner,我真的很想爲我的兩個.vbs代碼實現這種技術,這可以真正以這種方式開始嗎?如果我用我的兩個人來實現這個範式,我還需要更多的關注嗎? :-) –