Question

how to make a vbscript sleep w/o pinning the cpu

Asked by: tomandlis

I am trying to make a client-side vbscript sleep but can't do it w/o pinning the cpu

The Code is below, pretty straight forward, but I get the following error:

error: object doesn't support this property or method 'Wscript.Sleep'

***********code
<html>
<head>
<SCRIPT LANGUAGE="vbscript" >
      function bSleep()
            document.All.whatsup.innerHTML = "I'm sleepy"
            Set WScript = CreateObject("WScript.Shell")
            WScript.Sleep 2000
            document.All.whatsup.innerHTML = "Done sleeping"
      end function
</script>
<body onload="call bSleep()">
<div id="whatsup"></div>
</body>
</html>

***********end code

To workaround, I swap out the wscript.sleep and use a loop, but it pins the cpu and that isn't acceptable because I am sleeping so other stuff can happen in the background.  Here is the loop code that pins the cpu:

***********code  snip (loop1)

StartTime=Now()
Do While Now() < StartTime + 8.0 / (3600.0 * 24.0)
      '      loop
Loop

***********end code snip

I also tried this loop:

***********code  snip (loop2)

Do While Not oBrowser.readyState = READYSTATE_COMPLETE
'      sometimes this fails--failure is unacceptable
Loop

***********end code snip

FYI, You're probably wondering how the oBrowser object got involved in loop2 (above).  If you want some background on the context. . . I am using this sleep code while a internetexplorer object navigates.  I've found that looping structure #2 doesn't work when a site is a slow loader, i.e., it will say the readystate is complete, but it really isn't!  Additionally, loop2 pins the cpu too and the spawned browser can't download anything.

Below is the complete code for my application if you want to put it all in context run this code below.  For this to work you have to set your ie default tools-options-security-default level to 'low'

***********all code

<html>
<head>
<SCRIPT LANGUAGE="vbscript" >
      function bUpdateAll()
            document.All.whatsup.innerHTML = "starting"
            sUrl = "http://www.aspfree.com"

            const READYSTATE_COMPLETE = 4
            on error resume next

            Set oBrowser = CreateObject("InternetExplorer.Application")

            if err.number <> 0 then            
                  document.All.whatsup.innerHTML = "set your ie default tools-options-security-default level to 'low'"
                  exit function
            end if

            on error goto 0

            oBrowser.Width = 600
            oBrowser.Height = 400
            oBrowser.Left = 50
            oBrowser.Top = 50
            oBrowser.Visible = true      
            oBrowser.Silent = true

            oBrowser.Navigate(sUrl)
            StartTime=Now()
            Do While Not oBrowser.readyState = READYSTATE_COMPLETE
                  '      loop
            Loop
            set oBrowser = nothing
            document.All.whatsup.innerHTML = "done"
      end function
</script>
</head>
<body onload="call bUpdateAll()">
      <div id="whatsup"></div>
</body>
</html>

***********end all code

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2004-08-19 at 07:42:45ID21099829
Tags

sleep

,

vbscript

Topic

Active Server Pages (ASP)

Participating Experts
5
Points
500
Comments
22

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. VBScript - Not getting any output  (Wscript.shell  and W…
    I am not getting any output to the screen with the following script. I am not sure what I have done wrong. I am use to doing things server side instead of client side. Thanx, Soren <html> <head> </head> <body> <script type="...
  2. Controlling RS232 PINS Status
    i need to control an electronical relay that get's logical 0(GND) or 1(5Volts) my idea was to give that data using rs232 which i used a little bit before with motorola's assembly, i need to be able to control the status of only one pin of the connector no need for input readi...
  3. using WScript.Shell
    oWsh = CreateObject("WScript.Shell") cDesktopDir = oWsh.SpecialFolders("Desktop") oLnk = oWsh.CreateShortcut(cDesktopDir + "\\Shortcut to Notepad.lnk") oLnk.TargetPath = oWsh.ExpandEnvironmentStrings("%windir%") + "\notepad.exe&quo...
  4. Pin document to Start Menu?
    I am trying to create a vbs script to "pin" a word document to the start menu for all users on the network. I've tried the following but does not seem to work.: Set objShell = CreateObject("Shell.Application") Set objFolder = objShell.Namespace("C:\&...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: fritz_the_blankPosted on 2004-08-19 at 07:53:23ID: 11842000

In general, you don't want to do this. It is much better to arrange your code in such a way this isn't necessary.

FtB

 

by: alorentzPosted on 2004-08-19 at 07:58:00ID: 11842058

You're trying a server script in client side script.  Would need to be:

<%
     function bSleep()
          Set WScript = Server.CreateObject("WScript.Shell")
          WScript.Sleep 2000
      end function
%>

What you are trying is not possible.  Client side VB will not control sevrer side sleep.  Do you understand the differecne?

 

by: BChanPosted on 2004-08-19 at 08:21:19ID: 11842375


What are you trying to do? if it is client side, and you can use javascricpt, you can use the setTimeout method

self.setTimeout('method()', 5000)  //-- Call method() in 5000 miliseconds

http://www.devguru.com/Technologies/ecmascript/quickref/win_settimeout.html

 

by: GoofyDawgPosted on 2004-08-19 at 08:47:07ID: 11842655

You can't do this in ASP because by its very nature, it is single-threaded, and interpreted at runtime. In other words, you can spark up a process as you've done above, wait for it, but you can't go beyond the wait. The only way to effectively do this is to build a multi-threaded COM or ActiveX object that does the processing in the background that periodically checks for the ready state transmitted from the client to the server (though I'm not sure if WScript can actually call WinAPI functions where you can call CreateThread yourself). The object in turn will transfer processing control back to your script once your ready state is achieved.

GoofyDawg

 

by: tomandlisPosted on 2004-08-19 at 09:07:13ID: 11842873

BChan: yes, I am doing this client side.  Go ahead and copy the code into a asp page and it will work.

GoofyDawg:  In the '***********all code' section in my original question (above) I believe the internetexplorer.application object is asynchronous, i.e., I spawn it and it runs side by side with my vbscript code.  

I think, the problem stems from the fact that the vbscript code will pin the cpu when running the 'do while' loop, thus the spawned browser can't get many cpu cycles to download content.  If the vbscript code could just sleep for a second and give up some cycles then the spawned browser could finish its work (note, the 'all code' will work, as is, but sometimes not when you're navigating to a really slow site it fails--which is my problem).

 

by: tomandlisPosted on 2004-08-19 at 09:10:11ID: 11842912

Bchan, is there a way to do the settimeout in vbscript?  I'd rather not convert all my code to javascript

alorentz, oh, I didn't realize that wscript wasn't available on the client side.  But, if that is so, then why doesn't the script bomb when I create the wscript object?

 

by: BChanPosted on 2004-08-19 at 09:14:09ID: 11842951

 

by: fritz_the_blankPosted on 2004-08-19 at 09:14:46ID: 11842964

When you execute a wscript shell, it is running on the server, not the client machine. Otherwilse, I could call a script that would delete files on a client's machine and etc.

This brings me back to my first point--it is best not to build something that is going to require a wait as this is a bad situation for the server to be in.

FtB

 

by: BChanPosted on 2004-08-19 at 09:31:16ID: 11843147

tomandlis,
Why not use the window.open method?
window.open("Sample.htm",null,
    "height=200,width=400,status=yes,toolbar=no,menubar=no,location=no");
What is the goal of your script?

You might be able to use this, or another approacr, such as an iframe. You then will not need to have the security settings change.

 

by: tomandlisPosted on 2004-08-19 at 09:52:38ID: 11843425

bchan, I believe window.open is a javascript function.  Please note that this is in vbscript.

If you really want to know the purpose of this script is essentially screen scraping

1. have the client side browser go to a site
2. navigate (click) a few links on that site to get to a destination page
3. gather info from the destination page
4. submit the gathered information via http to my servers

simple huh

I do step 4 by spawning a new browser and constructing a simple form (containing the gathered information) that has its 'action' set to a receiving page on my site.  

Why do this you ask?  Simple, because the screen scaping can only be done from a client-side browser that accepts cookies.--The site being scrapped is password protected.

 

by: fritz_the_blankPosted on 2004-08-19 at 10:14:08ID: 11843707

I wonder if there isn't any way to use something like this for scraping:

Function GetHTML(strURL)
      Dim objXMLHTTP, strReturn
      Set objXMLHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP")
      objXMLHTTP.Open "GET", strURL, False
      objXMLHTTP.Send
      strReturn = objXMLHTTP.responseText
      Set objXMLHTTP = Nothing
      GetHTML = strReturn
End Function

FtB

 

by: tomandlisPosted on 2004-08-19 at 10:18:49ID: 11843761

fritz, yes your code will work.  However, it doesn't work very well with cookies because some pages will check to see if you have a cookie (password protected site).  Consequently, it is better to just click links using a internetexplorer object when scraping pwd protected sites.

Also, your script only works server side and that will put a drag on the server.  Better to distribute something like this to the client.

 

by: tomandlisPosted on 2004-08-19 at 10:24:27ID: 11843832

Anyway, we've gotten a little off track here.  Remember, the original question is rather simple-- How can you get a client side vbscript to sleep while browsing with an internetexplorer activex object ?

So far the answers seem to be you can't unless you convert it to javascript, but I'll need an example of that (that doesn't pin the cpu) to accept that answer.

BTW, if you can remove the internetexplorer object from the equation (and still scape contents) I'll accept that too.

 

by: fritz_the_blankPosted on 2004-08-19 at 10:26:24ID: 11843858

Still going with my original answer: don't do it--program around it instead.

FtB

 

by: BChanPosted on 2004-08-19 at 11:05:09ID: 11844368

You can call javascript function from a vb:

<html>
<head>
<SCRIPT LANGUAGE="vbscript" >
     function bUpdateAll()
          document.all.whatsup.innerHTML = "starting"
          sUrl = "http://www.aspfree.com"

          const READYSTATE_COMPLETE = 4
          on error resume next

          Set oBrowser = CreateObject("InternetExplorer.Application")

          if err.number <> 0 then          
               document.all.whatsup.innerHTML = "set your ie default tools-options-security-default level to 'low'"
               exit function
          end if

          on error goto 0

          oBrowser.Width = 600
          oBrowser.Height = 400
          oBrowser.Left = 50
          oBrowser.Top = 50
          oBrowser.Visible = true    
          oBrowser.Silent = true

          oBrowser.Navigate(sUrl)
         bSleep() 'Call js function that sleeps
         ' StartTime=Now()
         ' Do While Not oBrowser.readyState = READYSTATE_COMPLETE
               '     loop
         ' Loop
          set oBrowser = nothing
          document.All.whatsup.innerHTML = "done"
     end function
</script>
</head>
<body onload="call bUpdateAll()">
     <div id="whatsup"></div>
</body>
</html>




<SCRIPT LANGUAGE="vbscript" >
   
     function FWake()
          document.all.whatsup.innerHTML = "Im awake"            

     end function
</script>
<SCRIPT LANGUAGE="javascript" >
 function bSleep(){

          document.all.whatsup.innerHTML = "Sleeping"            
            window.setTimeout( "FwAKE()", 5000); <!-- change 5000 to your wait time-->
                     

      }
</SCRIPT  >
<Div ID=whatsussp>NOTHING</Div >

 

by: alorentzPosted on 2004-08-19 at 11:09:44ID: 11844437

>>Anyway, we've gotten a little off track here.  Remember, the original question is rather simple-- How can you get a client side vbscript to sleep while browsing with an internetexplorer activex object ?

I agree - not a good idea, and will be unreliable regardless.

 

by: fritz_the_blankPosted on 2004-08-19 at 11:39:37ID: 11844805

>>Set oBrowser = CreateObject("InternetExplorer.Application")<<

Oh, please don't do that--Microsoft warns against it:

http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q257/7/57.ASP&NoWebContent=1

FtB

 

by: tomandlisPosted on 2004-08-19 at 12:15:50ID: 11845179

bChan!  You rock.  I used your suggestion.  Try the code below.  It works and it doesn't pin the cpu.  


<html>
<head>
<SCRIPT LANGUAGE="vbscript" >
      dim oBrowser
     function bUpdateAll()
          document.all.whatsup.innerHTML = "starting"
          sUrl = "http://www.aspfree.com"

          const READYSTATE_COMPLETE = 4
          on error resume next

          Set oBrowser = CreateObject("InternetExplorer.Application")

          if err.number <> 0 then          
               document.all.whatsup.innerHTML = "set your ie default tools-options-security-default level to 'low'"
               exit function
          end if

          on error goto 0

          oBrowser.Width = 600
          oBrowser.Height = 400
          oBrowser.Left = 50
          oBrowser.Top = 50
          oBrowser.Visible = true    
          oBrowser.Silent = true

          oBrowser.Navigate(sUrl)
         bSleep(1) 'Call js function that sleeps
         
          document.All.whatsup.innerHTML = "done"
     end function

      function FWake(linkcase)
            document.all.whatsup.innerHTML = "Im awake: " '& oBrowser.readyState
            EndTime = Now() + 5.0 / (3600.0 * 24.0)
            Do While Now() < EndTime and Not (oBrowser.readyState = READYSTATE_COMPLETE)
                  ' Empty loop forces script to wait.                  
            Loop
            if linkcase = 1 then
                  bTemp = bClickLink("utilize")
                  bSleep(2)
            else
                  document.all.whatsup.innerHTML = oBrowser.Document.body.innerHTML
            end if
      end function


      Function bClickLink(sHrefFrag)
            bComplete = true
            bClickLink = false
            Dim x , i
            x = oBrowser.Document.links.Length
            Dim s , sTheLink
            For Each oLink In oBrowser.Document.links
                  sTemp = sTemp & "<br/>link: " & oLink.getAttribute("href")
                  If InStr(1, oLink.getAttribute("href"), sHrefFrag, vbTextCompare) > 0 Then
                        document.All.whatsup.innerHTML = "<p>clicked: " & oLink.getAttribute("href")
                        oLink.Click
                        bClickLink = True
                        Exit For
                  End If
            Next
      End Function
</script>
<SCRIPT LANGUAGE="javascript" >
      function bSleep(theCase){
            sFunc = "FWake(" + theCase + ")";
            document.all.whatsup.innerHTML = "Sleeping";
            window.setTimeout( sFunc, 5000);
      }
</SCRIPT>
</head>
<body onload="call bUpdateAll()">
     <div id="whatsup">Nothing</div>
</body>
</html>

 

by: tomandlisPosted on 2004-08-19 at 12:19:02ID: 11845210

Fritz, the article says you shouldn't do it serverside.  The code above is client side.

 

by: tomandlisPosted on 2004-08-19 at 12:44:39ID: 11845485

hmm, I think I found a way to do it all within vbscript here:

http://www.codeproject.com/asp/webtimer.asp

 

by: tomandlisPosted on 2004-08-19 at 12:50:45ID: 11845551

Here is the code for the timer.  I think I'm going to use this code as it is a little cleaner, but BChan you were a big help and deserve the points.

Thanks

<HTML>
<HEAD>
<title>Demonstration</title>
<SCRIPT LANGUAGE="VBScript">
<!--
      Dim intTimerID
      hr=0            'change hours
      min=2            'change minutes
      sec=0            'change seconds
      
      Sub HandleTime
      if hr=0 and min=0 and sec=0 then
            endtime
      elseif min>=0 and sec>0 then
            sec=sec-1
            status=hr & ":" & min & ":" & sec
            intTimerID=setTimeOut("HandleTime",950, "VBScript")
      elseif min>0 and sec=0  then
            min=min-1
            sec=59
            status=hr & ":" & min & ":" & sec
            intTimerID=setTimeOut("HandleTime",950, "VBScript")
      elseif hr>=0 and min=0 then
            hr=hr-1
            min=59
            sec=59
            status=hr & ":" & min & ":" & sec
            intTimerID=setTimeOut("HandleTime",950, "VBScript")
      end if
      End Sub
      
      Sub endtime
            cleartimeout intTimerID
              status = "Your Time Ended"
            msgbox "Time Up"
      end sub

      status=hr & ":" & min & ":" & sec
      intTimerID=setTimeOut("HandleTime",950, "VBScript")
-->
</SCRIPT>
</HEAD>

<BODY>
      <h3>You’ll see a message box when the time is up.</h3>
      <hr>
      <br><input type="button" value="Click before time is up" onclick="call endtime">
</BODY>
</HTML>

 

by: daluuPosted on 2008-02-14 at 15:37:23ID: 20898024

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...