Solved

VBScript stops after 10 seconds in Windows 7

Posted on 2010-08-16
4
680 Views
Last Modified: 2012-05-10
I usually only write scripts for Windows servers and have just found that any script I run on my Win 7 PC runs OK unless the execution time is longer than 10 seconds.
I think I'm missing something obvious and suspect it's either a security setting or limitation I'm not aware of.
On my Win 7 PC the script below will just stop short of 10 seconds, but on a Win 2003 server it will complete.
Option Explicit
Dim WshShell, i, StartTime, ScriptTime
StartTime = Timer()
Set WshShell = WScript.CreateObject("WScript.Shell")

WshShell.Run "notepad"

WScript.Sleep 1000

WshShell.AppActivate "notepad"

For i = 1 To 100
	ScriptTime = FormatNumber(timer() - StartTime, 2)
	WshShell.SendKeys "Test "& i & " - " & ScriptTime & " seconds{ENTER}"
	WScript.Sleep 1000
Next

WScript.Quit

Open in new window

0
Comment
Question by:PMGreensted
  • 2
4 Comments
 
LVL 4

Expert Comment

by:Jamie_Wilson
ID: 33450526
On my W7 PC its fine, must be a local issue.

Might be a long shot, but try disabling UAC?
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 33450714
Hi, this works fine for me on Windows 7...
I haved added
 If WshShell.AppActivate("notepad") = True Then
because you don't want to be sending keystrokes to any application that is not the one you want in focus....
Regards,
Rob.

Option Explicit

Dim WshShell, i, StartTime, ScriptTime

StartTime = Timer()

Set WshShell = WScript.CreateObject("WScript.Shell")



WshShell.Run "notepad"



WScript.Sleep 1000



For i = 1 To 100

	If WshShell.AppActivate("notepad") = True Then

		ScriptTime = FormatNumber(timer() - StartTime, 2)

		WshShell.SendKeys "Test "& i & " - " & ScriptTime & " seconds{ENTER}"

		WScript.Sleep 1000

	End If

Next



WScript.Quit

Open in new window

0
 

Accepted Solution

by:
PMGreensted earned 0 total points
ID: 33453388
Good point about adding 'If WshShell.AppActivate("notepad") = True' although my script was only for testing the timout.

I've now played around and found I could get it working by running from the command line with the //T:nn      (Time out in seconds) switch set to a high enough value. e.g.

cscript C:\Sendkeys_Test.vbs //T:100

However, I've found the actual reason for the problem on my PC was the Windows Script Host settings in the registry:

[HKEY_CURRENT_USER\Software\Microsoft\Windows Script Host\Settings]
"Timeout"=dword:0000000a (10)
"DisplayLogo"=dword:00000001 (1)
"Flags"=dword:000003e8 (1000)

Increasing the Timeout obviously increases the default timeout if I were to run a script without a switch.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 33459501
Good find. You could also run
wscript //T:00 //S
to save a zero time-out for your current user settings.
Regards,
Rob.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
In this Micro Tutorial viewers will learn how to use Boot Corrector from Paragon Rescue Kit Free to identify and fix the boot problems of Windows 7/8/2012R2 etc. As an example is used Windows 2012R2 which lost its active partition flag (often happen…
This Micro Tutorial will give you a introduction in two parts how to utilize Windows Live Movie Maker to its maximum editing capability. This will be demonstrated using Windows Live Movie Maker on Windows 7 operating system.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now