HTA脚本的睡眠例程 [英] Sleep routine for HTA scripts

查看:100
本文介绍了HTA脚本的睡眠例程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我创建的几个.HTA脚本中,我需要VBScript WScript.Sleep命令,该命令仅等待数毫秒而不使用CPU。
而且,当我浏览Web时,似乎并不是我唯一在寻找它的人:

In several of my .HTA scripts that I created, I had the need for the VBScript WScript.Sleep command which simply waits for a number of milliseconds without utilizing the CPU. And when I browse the web, it appears that I am not the only one looking for this:

https://www.google.nl/search?q=hta+sleep

(我敢打赌,如果您读过此书,您可能也需要此书)

(I bet that if you read this, you probably need(ed) this as well)

我能找到的最佳解决方案似乎是使用PING命令。
但是,特别是在仅需要将脚本暂停几百毫秒的情况下,此解决方案非常奇怪,因为它使用外部命令并触发所有可能与相关问题无关的(网络)进程.HTA脚本。

The best solution that I could find appears to be the one which uses the PING command. But especially for a situation were just need to pause the script for a few 100ms, this solution is quiet odd as it uses an external command and triggers all kind of (network) processes that unlikely have anything to do with the concerned .HTA script.

所以我想到的第一件事是使用WMI Win32_PingStatus类来避免使用外部命令,但是随后我开始质疑为什么不完全基于在WMI上。
我花了几个小时才能找到正确的WMI类和方法,但最终我成功了……

So the first thing that came to my mind was to use the WMI Win32_PingStatus class to avoid the external command but then I started to question why not completely basing it on WMI. It has taken me several hours to get the right WMI classes and methods in place, but finally I succeeded…

推荐答案

编写HTA时,您应该异步考虑。考虑使用 window.setTimeout 重写代码。在下面的示例中,我将使用 window.setTimeout 每2秒发出一次铃声:

When writing HTA's you should be thinking asynchronously. Consider rewriting your code to use window.setTimeout. In the following example, I will use window.setTimeout to make a bell sound every 2 seconds:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="x-ua-compatible" content="ie=8">
<title>Bell Test</title>
<script language="VBScript">
Option Explicit
Dim objWShell
Set objWShell = CreateObject("WScript.Shell")
Sub DoPing
    divText.innerText = Now
    objWShell.Run "%COMSPEC% /c ECHO " & Chr(7), 0, False
    window.setTimeOut "DoPing", 2000
End Sub
Sub window_OnLoad
   window.ResizeTo 240,130
   DoPing
End Sub
</script>
</head>
<body>
<div id="divText">TEST</div>
</body>
</html>

这篇关于HTA脚本的睡眠例程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆