VBScript - Capturing output from stdout

爷,独闯天下 提交于 2019-12-05 19:55:16

The problem is that your code does not end, returning the control to the browser. You don't leave the loop until the program ends and the perceived status is that the interface hangs until the subprocess ends.

You need to set a callback so the browser will periodically call your code where you will update the status and leave.

<html>
<head>
    <title>pingTest</title>
    <HTA:APPLICATION
        APPLICATIONNAME="pingTest"
        ID="pingTest"
        VERSION="1.0"
    />
</head>

<script language="VBScript">
    Const WshRunning = 0
    Const WshFinished = 1
    Const WshFailed = 2

    Dim WshShellExec, Interval

    Sub Window_onLoad
        LaunchProcess
    End Sub

    Sub LaunchProcess
        Set WshShellExec = CreateObject("WScript.Shell").Exec("ping -n 10 127.0.0.1")
        Interval = window.setInterval(GetRef("UpdateStatus"),500)
    End Sub    

    Sub UpdateStatus
    Dim status 
        Set status = Document.GetElementByID("status")
        Select Case WshShellExec.Status
            Case WshRunning
                status.InnerHTML = status.InnerHTML & "<br>" & WshShellExec.StdOut.ReadLine()
            Case WshFinished, WshFailed
                status.InnerHTML = status.InnerHTML & "<br>" & Replace(WshShellExec.StdOut.ReadAll(),vbCRLF,"<br>")
                window.clearInterval(Interval)
                Interval = Empty
        End Select
    End Sub
</script>

<body>
    <div id="status"></div>
</body>
</html>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!