Windows 7 - How to cancel script running

Asked By VijayKotia on 22-Aug-07 07:32 AM
If I would like to cancel the script while running how this can be done or is
it required to mentione  somewhere in program then how it can be done.

Pegasus \(MVP\) replied on 22-Aug-07 07:42 AM
Pressing Ctrl+C or Ctrl+Break will usually terminate a script.
VijayKotia replied on 22-Aug-07 08:54 AM
Thank you for your reply.  I regret to inform you that it is not cancelling
by  Pressing Ctrl+C or Ctrl+Break.  My scripts are saved as .wsf and run the
same but some times i face problem with script and unable to cancel while
running.  Please provide me with any othe altenatives.  Thank you in
anticipation for your quick reply
Pegasus \(MVP\) replied on 22-Aug-07 11:01 AM
Ctrl+C and Ctrl+Break are emergency exits. If you need to
break out of your scripts routinely then you should include
break-out points at appropriate places, perhaps coupled with
a "sleep" instruction.

As an alternative you can use the Windows Task Manager
to terminate your programs. Not nice . . .
TDM replied on 22-Aug-07 12:22 PM
In order to use CTRL+C to cancel, you must run the wsf from a shell.
It is also best to set your default shell to CScript as well.

If you are simply double clicking from the Windows Explorer, and your
default script engine is not CScript,  then your only options are to put a
cancel hook in the script, or kill it from the task manager processes list.

Here is a simpl cancel hook that may help you get started.

Dim objIEItems
Dim objOKapp
Dim bOKPressed
Dim x

Call genIEPage()

Do While Not bOKPressed
objIEItems.Item("Ok").Onclick = GetRef("okPressed")
WScript.Sleep 100
WScript.Echo "Taterboy " & x
x = x + 1
'Your code goes here

Function genIEPage

Set objOKapp = WScript.CreateObject("InternetExplorer.Application")

With objOKapp
.ToolBar = False
.TheaterMode = False
.StatusBar = False
.AddressBar = False
.Resizable = True
.Visible = True
.Height = 50
.Width = 50
.Left = .Document.ParentWindow.Screen.Width / 2 - .Width / 2
.Top = .Document.ParentWindow.Screen.Height / 2 - .Height / 2
With .Document
.WriteLn("<TITLE>OK Button Test</TITLE>")
.WriteLn("<FORM NAME=""okForm"">")
.WriteLn("<table align=""center""><tbody>")
Set objIEItems = .All
End With
End With

End Function

Function okPressed()
bOKPressed = True
WScript.Echo "Script terminated by user ..."
End Function

Paul Randall replied on 22-Aug-07 01:26 PM
I often use Pegasus' method.  It never causes me problems if it is the only
instance of WScript.exe running, but when two or more are running it can be
difficult to figure out which one to terminate.

I often use TDM's method.  I guess I should quit being so lazy and build a
nice function or subroutine to do this and put it in my standard script
initialization/library-setup routine.

-Paul Randall