问题描述
我知道这已经在另一个问题中得到了回答,但我根本不明白它是如何完成的.
I know this has been answered one in another question, but I simply do not understand how it is done.
我正在尝试将命令行程序(Aria2 下载器)的输出放入 HTA 脚本中,以便对其进行解析,并且可以获取下载百分比、文件大小等并将其动态更新为 DIV.
I am trying to get the output of a command line program (Aria2 downloader) into a HTA script so it can be parsed and the download percentage, file size etc can be obtained and updated into a DIV dynamically.
这是我已经调整并尝试使用的代码,但它只是锁定界面,直到命令行完成,然后显示所有输出,而不是在它通过时显示.
Here is the code I have adjusted and have been trying to use but it just locks up the interface until the command line has finished and THEN displays all the output, instead of displaying it as and when it comes through.
Const WshRunning = 0
Const WshFinished = 1
Const WshFailed = 2
strCommand = "ping.exe 127.0.0.1"
Set WshShell = CreateObject("WScript.Shell")
Set WshShellExec = WshShell.Exec(strCommand)
Do While WshShellExec.Status = WshRunning
window.setTimeOut "", 100
Loop
Select Case WshShellExec.Status
Case WshFinished
strOutput = WshShellExec.StdOut.ReadAll()
Case WshFailed
strOutput = WshShellExec.StdErr.ReadAll()
End Select
Set objItem = Document.GetElementByID("status")
objItem.InnerHTML = "" & strOutput & ""
如何修改它以使其不会锁定我的用户界面并抓取输出并将其显示在状态"div 中?
How do I modify this so that it doesn't lock up my user interface and grabs the output and displays it in the "status" div as it comes through?
推荐答案
问题是你的代码没有结束,将控件返回给浏览器.在程序结束之前您不会离开循环,并且感知状态是接口挂起直到子进程结束.
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>
这篇关于VBScript - 从标准输出捕获输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!