Sleep routine for HTA scripts
Solution 1
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>
Solution 2
I had the same problem with HTA. My solution with vbs ...
Sub sleep (Timesec)
Set objwsh = CreateObject("WScript.Shell")
objwsh.Run "Timeout /T " & Timesec & " /nobreak" ,0 ,true
Set objwsh = Nothing
End Sub
' example wait for 3 seconds
sleep 3
The routine will call a shell command, minimized and without a keyboard command. Only ^C is permitted, but this will no user given in these situation.
Solution 3
Sub Sleep(iMilliSeconds)
With GetObject("winmgmts:\\.\root\cimv2")
With .Get("__IntervalTimerInstruction").SpawnInstance_()
.TimerId = "Sleep"
.IntervalBetweenEvents = iMilliSeconds
.Put_()
End With
.ExecNotificationQuery("SELECT * FROM __TimerEvent WHERE TimerId='Sleep'").NextEvent
End With
End Sub
Added 2015-02-11:
Unfortunately, this function doesn’t work when using Internet Explorer 10 (see comments below). With Internet Explorer 11 installed, it appears to work if you run the HTA as administrator.
iRon
There are only 10 types of people in the world: those who understand binary, and those who don't
Updated on June 14, 2022Comments
-
iRon over 1 year
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)
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.
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…
-
Teemu over 10 yearsInteresting, but I get
Access denied
when executing.Put_()
. Any ideas...? -
iRon over 10 yearsai, I recognise this from some other HTAs with embedded WMI calls. It is Windows 8 (I have created the snipped in Windows 7 and indeed just confirmed the error in Windows 8). The workarround is to run it as an administrator, but that makes to whole solution less interesting. In need to investigate, or if anybody else know how to get arround this?
-
Teemu over 10 yearsActually I'm using Windows7 (with IE10). This WMI code is working within the app I tested your
sleep()
function. I tried with VBS and also translated your code to JavaScript as well, but both ways I getAccess denied
error. -
iRon over 10 yearsYou're right the difference is in fact IE, IE8 works fine... (I will further investigate this next week)
-
Teemu over 10 yearsWorks also with IE9, but not with IE10. I tried to play around with WMI security but no results. It'll take time to get familiar with all that stuff, it seems to be quite complicated...
-
iRon over 10 yearsI have done quiet some investigation and testing today but unfortunately haven't found a solution for IE10. Personally, I don't think that it is correct that I need elevated rights to set a timer...
-
iRon over 10 yearsI filed a bug report for IE10 at Microsoft: connect.microsoft.com/IE/feedback/details/789421