Solved

VBScript stops after 10 seconds in Windows 7

Posted on 2010-08-16
4
684 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

New Windows 7 Installations take days for Windows-Updates to show up and install. This can easily be fixed. I have finally decided to write an article because this seems to get asked several times a day lately. This Article and the Links apply to…
A quick guide on how to use Group Policy to create a custom power plan and set it active on Windows 7.
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…

730 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