Solved

VBScript stops after 10 seconds in Windows 7

Posted on 2010-08-16
4
679 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Hi Friends, These registry tweaks will help you optimizing your Windows 7 system for any VDI. This will improve the machine performanance and can be used on normal systems also. These are few registry tweaks which will add value by enhancing the …
OfficeMate Freezes on login or does not load after login credentials are input.
This Micro Tutorial will teach you the basics of configuring your computer to improve its speed. It will also teach you how to disable programs that are running in the background simultaneously. This will be demonstrated using Windows 7 operating…
This Micro Tutorial will give you basic overview of the control panel section on Windows 7. It will depth in Network and Internet, Hardware and Sound, etc. This will be demonstrated using Windows 7 operating system.

744 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

16 Experts available now in Live!

Get 1:1 Help Now