Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Sleep in HTA: not a dream anymore!

 
Logged in as: Guest
arrSession:exec spGetSession 2,16,48600
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> Post a VBScript >> Sleep in HTA: not a dream anymore!
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Sleep in HTA: not a dream anymore! - 6/21/2007 9:26:10 AM   
  Fredledingue


Posts: 337
Score: 0
Joined: 5/9/2005
From:
Status: offline
The TechGuy tells us to use the setInterval method... no no no: Tha'ts not a "Sleep".

A real Sleep is this:
(the code is not from me but from some dude who posted it on a another forum, I lost the link)


      


_____________________________

Fred
 
 
Post #: 1
 
 RE: Sleep in HTA: not a dream anymore! - 6/21/2007 11:44:53 AM   
  dm_4ever


Posts: 2433
Score: 38
Joined: 6/29/2006
From: Orange County, California
Status: offline
Using an external VBS file to do the sleeping has always been my favorite way to achieve this.

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to Fredledingue)
 
 
Post #: 2
 
 RE: Sleep in HTA: not a dream anymore! - 6/22/2007 8:46:38 AM   
  Fredledingue


Posts: 337
Score: 0
Joined: 5/9/2005
From:
Status: offline
It's the kind of trick which is very simple, very easy to code but, for some reason, you never thought about before.

_____________________________

Fred

(in reply to dm_4ever)
 
 
Post #: 3
 
 RE: Sleep in HTA: not a dream anymore! - 6/25/2007 5:31:22 AM   
  TNO


Posts: 1066
Score: 10
Joined: 12/18/2004
From: thenewobjective.com
Status: offline
Wouldn't this method work a well?

quote:

<html>
<head>
<script type="text/vbscript">

Sub Sleep(t,i)

  MyTime = DateAdd(i,t,Now)

  While Now < MyTime
      'Do Nothing and Loop
  Wend

MsgBox "Done"
 
End Sub

</script>
</head>
<body>
<button onclick="Sleep 5,'s'">Go</button>
</body>
</html>


_____________________________

To iterate is human, to recurse divine. -- L. Peter Deutsch

(in reply to Fredledingue)
 
 
Post #: 4
 
 RE: Sleep in HTA: not a dream anymore! - 6/25/2007 10:39:34 AM   
  Fredledingue


Posts: 337
Score: 0
Joined: 5/9/2005
From:
Status: offline
Yes and no.
It will wait but these lines...

  While Now < MyTime
      'Do Nothing and Loop
  Wend

...will cause a 100% processor usage during all the waiting time.
(I'v tested it)
Other crazy methods using a "ping" command line, also cause the same processor activity.

This is not the intended effect of a "sleep". usualy you want a sleep when the computer needs resources for another task. Not to block these resources in a loop.
The irony is that you can't put "WScript.sleep" inside the loop to avoid that!
Only the swcript.sleep methods realy pauses the script.
The downside of it in Hta is that everytime you want a sleep, you have to launch wscript.exe which is not very low-resource friendly. Especialy if you want very short sleeps, like 50 milliseconds. But it works well for larger sleeps (500 ms or more).

< Message edited by Fredledingue -- 6/25/2007 11:12:34 AM >


_____________________________

Fred

(in reply to TNO)
 
 
Revisions: 1 | Post #: 5
 
 RE: Sleep in HTA: not a dream anymore! - 6/26/2007 5:03:03 AM   
  DiGiTAL.SkReAM


Posts: 1157
Score: 7
Joined: 9/6/2005
From: Florida, USA
Status: offline
quote:

ORIGINAL: Fredledingue
The TechGuy tells us to use the setInterval method... no no no: Tha'ts not a "Sleep".

A real Sleep is this:
<snipped shell.Run method>


Just out of curiosity, why do you say that the setInterval is not a "Sleep"?  The CPU usage doesn't spike as it does when you run an external script...  sure you ahve to be careful with your code, but it works...
Having used both methods in HTAs, I think i like the setinterval method better, due to the lower amount of code/OS interaction required.

_____________________________

"Would you like to touch my monkey?" - Dieter (Mike Meyers)

"It is better to die like a tiger, than to live like a pussy."
-Master Wong, from Balls of Fury

(in reply to Fredledingue)
 
 
Post #: 6
 
 RE: Sleep in HTA: not a dream anymore! - 6/26/2007 5:54:44 AM   
  Fredledingue


Posts: 337
Score: 0
Joined: 5/9/2005
From:
Status: offline
The setInterval is not a Sleep: it's a repetition of a subroutine.
You can't make a script sleeping with that method: you can only launch a script after a given interval.
To imitate a sleep with setInterval you need to slice your code into as many subs as you have sleeps and clear the interval after the sub has ran once.
Technicaly that's better if you have only one or two sleep lines in your code, not if you have two dozen of them and, even worse, don't know exactely where to position them in the line order.
I had the case with a very long script where I had to test the sleep a few line up or a few lines down. Very difficult to do with setInterval.

Note that the setInterval can be very useful in other cases. To set up a progressbar for example.

_____________________________

Fred

(in reply to DiGiTAL.SkReAM)
 
 
Post #: 7
 
 RE: Sleep in HTA: not a dream anymore! - 6/26/2007 1:50:47 PM   
  wanghuok


Posts: 17
Score: 0
Joined: 10/13/2005
Status: offline
Why not use wscript.sleep directly,

What is the advantage as you done.

_____________________________

I come from China,This is a very beautiful country.

Welcome to China.

(in reply to Fredledingue)
 
 
Post #: 8
 
 RE: Sleep in HTA: not a dream anymore! - 6/26/2007 2:06:13 PM   
  dm_4ever


Posts: 2433
Score: 38
Joined: 6/29/2006
From: Orange County, California
Status: offline
Look at the title of the post: Sleep in HTA: not a dream anymore!

You can't use WScript.Sleep inside a .hta or .htm/.htm file.

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to wanghuok)
 
 
Post #: 9
 
 RE: Sleep in HTA: not a dream anymore! - 6/26/2007 8:04:53 PM   
  wanghuok


Posts: 17
Score: 0
Joined: 10/13/2005
Status: offline
Oh.Thank you

I understand.

_____________________________

I come from China,This is a very beautiful country.

Welcome to China.

(in reply to dm_4ever)
 
 
Post #: 10
 
 RE: Sleep in HTA: not a dream anymore! - 6/27/2007 12:19:00 AM   
  DiGiTAL.SkReAM


Posts: 1157
Score: 7
Joined: 9/6/2005
From: Florida, USA
Status: offline
quote:

ORIGINAL: Fredledingue
The setInterval is not a Sleep: it's a repetition of a subroutine.
You can't make a script sleeping with that method: you can only launch a script after a given interval.
To imitate a sleep with setInterval you need to slice your code into as many subs as you have sleeps and clear the interval after the sub has ran once.
Technicaly that's better if you have only one or two sleep lines in your code, not if you have two dozen of them and, even worse, don't know exactely where to position them in the line order.
I had the case with a very long script where I had to test the sleep a few line up or a few lines down. Very difficult to do with setInterval.


Good point, hadn't thought of it that way.

quote:

ORIGINAL: Fredledingue
Note that the setInterval can be very useful in other cases. To set up a progressbar for example.




_____________________________

"Would you like to touch my monkey?" - Dieter (Mike Meyers)

"It is better to die like a tiger, than to live like a pussy."
-Master Wong, from Balls of Fury

(in reply to Fredledingue)
 
 
Post #: 11
 
 RE: Sleep in HTA: not a dream anymore! - 6/27/2007 5:14:38 PM   
  ginolard


Posts: 1024
Score: 21
Joined: 8/10/2005
Status: offline
Seems a very roundabout way of doing things to me.  I always just used ping


      

_____________________________

Author of ManagePC - http://managepc.net
AD Query Template - http://www.visualbasicscript.com/m_40609/tm.htm
Consolidated Scripting Framework - http://www.visualbasicscript.com/m_59109/tm.htm

(in reply to DiGiTAL.SkReAM)
 
 
Post #: 12
 
 RE: Sleep in HTA: not a dream anymore! - 6/28/2007 12:54:27 AM   
  DiGiTAL.SkReAM


Posts: 1157
Score: 7
Joined: 9/6/2005
From: Florida, USA
Status: offline
While I am not certain that number of pings = time in seconds, I will agree that it does NOT cause 100% CPU usage... unless maybe you are running on a 386/16, in which case, you have more problems than trying to get a sleep function in an HTA.


_____________________________

"Would you like to touch my monkey?" - Dieter (Mike Meyers)

"It is better to die like a tiger, than to live like a pussy."
-Master Wong, from Balls of Fury

(in reply to ginolard)
 
 
Post #: 13
 
 RE: Sleep in HTA: not a dream anymore! - 6/29/2007 4:30:42 AM   
  Fredledingue


Posts: 337
Score: 0
Joined: 5/9/2005
From:
Status: offline
I tested the cod posted by Gignolar:
There is a spike to 100% during one second or two then, cpu usage is back to normal or only lightly higher.

Previousely I tried the address: 1.1.1.1, as given in the example where I found it.
With this address there is a cpu use of 100% all the time.
So the address you use is important.

However xith
WshShell.Run "sleeper.vbs " & strSeconds, 1, true

there is no detectable cpu use increase. So I conclude that it's still a better option.

_____________________________

Fred

(in reply to DiGiTAL.SkReAM)
 
 
Post #: 14
 
 RE: Sleep in HTA: not a dream anymore! - 6/29/2007 5:33:22 AM   
  DiGiTAL.SkReAM


Posts: 1157
Score: 7
Joined: 9/6/2005
From: Florida, USA
Status: offline
The point was to ping the lookback address.  You aren't really looking for any response, you are just trying to get something to take up some time.  So, rather than ping an address that you may or may not have access to, ping 127.0.0.1 - as long as you have a nic and Ip installed, it'll be there.

_____________________________

"Would you like to touch my monkey?" - Dieter (Mike Meyers)

"It is better to die like a tiger, than to live like a pussy."
-Master Wong, from Balls of Fury

(in reply to Fredledingue)
 
 
Post #: 15
 
 RE: Sleep in HTA: not a dream anymore! - 7/1/2007 6:52:51 PM   
  ginolard


Posts: 1024
Score: 21
Joined: 8/10/2005
Status: offline
Also, this is a Sleep function we're talking about.  How often do you need to sleep for exactly x number of seconds??  More often than not you just want to sleep for enough time that whatever was running, finishes running.  The ping method may not run for exactly the number of seconds you specify, I don't know, I never tested it.  But if I say "sleep for 5 seconds" and it sleeps for 6, I really don't mind.

_____________________________

Author of ManagePC - http://managepc.net
AD Query Template - http://www.visualbasicscript.com/m_40609/tm.htm
Consolidated Scripting Framework - http://www.visualbasicscript.com/m_59109/tm.htm

(in reply to DiGiTAL.SkReAM)
 
 
Post #: 16
 
 RE: Sleep in HTA: not a dream anymore! - 7/6/2007 11:00:55 AM   
  Fredledingue


Posts: 337
Score: 0
Joined: 5/9/2005
From:
Status: offline
From the few tests I made, the time was relatively acurate, at human scale.

_____________________________

Fred

(in reply to ginolard)
 
 
Post #: 17
 
 RE: Sleep in HTA: not a dream anymore! - 7/27/2007 9:38:30 AM   
  Rischip


Posts: 468
Score: 2
Joined: 3/26/2007
Status: offline
I like the method in the original post.
But for anyone that likes to really kludge things up, this one is for you.
It does spike the CPU, but all systems I ran it on were identically responsive as when it was not running.
It also gives a countdown.


      

< Message edited by Rischip -- 7/27/2007 9:49:44 AM >


_____________________________

Rischip
Author of - The Grim Linker

(in reply to Fredledingue)
 
 
Revisions: 2 | Post #: 18
 
 RE: Sleep in HTA: not a dream anymore! - 8/3/2007 12:45:39 PM   
  Fredledingue


Posts: 337
Score: 0
Joined: 5/9/2005
From:
Status: offline
Yet another method (experimental):
Works very well for very short intervals, with an error margin of 10 msec.
I shows a popup but with very short interval you won't see it or barely. For long intervals (more than 3 seconds), it can be useful and dangerous at the same time.
Useful because the user knows that the script has to wait for something and dangerous because if the user decides to click on "ok" he can crash the script...
There is also an issue with selected/focused buttons which get triggered but it can be fixed with a switch. I'm still not sure where this switch must be so I post an example without it.


      

_____________________________

Fred

(in reply to Rischip)
 
 
Post #: 19
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> Post a VBScript >> Sleep in HTA: not a dream anymore! Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts