Discussion:
Pause a running script
(too old to reply)
Kryten
2010-03-08 15:11:54 UTC
Permalink
Hi,

A while back I wrote a pretty trivial script ( -ok all
my scripts are trivial, alright! ).

All it did was take strings from one place, modify them
and send them someplace else. Mundane stuff.

The users love it though as it saves them so much time.

One of the users has now asked me how to "pause" it.
Hhhmmm.. It's just iterating through an array of strings,
and using wscript.shell sendkeys to send the text into
another application.

How can I add functionality so that if the spacebar, for
example, is tapped the script pauses till the same key
is pressed again? I mean, it could be mid-pipeline...

Any ideas?

Cheers,
Stuart
Marco Shaw [MVP]
2010-03-08 15:26:40 UTC
Permalink
A very, very simple example:

PS>"foo","bar"|foreach{read-host "Continue";$_}

Not exactly what you're looking for, but I'm short on time right now.

Marco
Post by Kryten
Hi,
A while back I wrote a pretty trivial script ( -ok all
my scripts are trivial, alright! ).
All it did was take strings from one place, modify them
and send them someplace else. Mundane stuff.
The users love it though as it saves them so much time.
One of the users has now asked me how to "pause" it.
Hhhmmm.. It's just iterating through an array of strings,
and using wscript.shell sendkeys to send the text into
another application.
How can I add functionality so that if the spacebar, for
example, is tapped the script pauses till the same key
is pressed again? I mean, it could be mid-pipeline...
Any ideas?
Cheers,
Stuart
Kryten
2010-03-08 21:00:39 UTC
Permalink
Thanks Marco,

That would be prompting the user to continue
between each element in the array is processed
by the pipeline.

That wouldn't really be practical, as sometimes
there could be be a thousand items being processed
and it would be inefficient, and annoying.

I was thinking more along the lines of scanning the
keyboard for physical keystrokes without actually
resorting to read-host? At a push it would be ok
to only listen for input as certain points in the
script but without actually requiring a prompt to
continue.

Any thoughts?

Thanks,
Stuart
Zardos42
2010-03-09 00:43:01 UTC
Permalink
I use $host.UI.Raw.Readkey() | out-null
for this type of thing
Post by Kryten
Thanks Marco,
That would be prompting the user to continue
between each element in the array is processed
by the pipeline.
That wouldn't really be practical, as sometimes
there could be be a thousand items being processed
and it would be inefficient, and annoying.
I was thinking more along the lines of scanning the
keyboard for physical keystrokes without actually
resorting to read-host? At a push it would be ok
to only listen for input as certain points in the
script but without actually requiring a prompt to
continue.
Any thoughts?
Thanks,
Stuart
.
Marco Shaw [MVP]
2010-03-09 02:55:05 UTC
Permalink
See Zardos' comment... I just didn't remember the path. I'll check it out
tomorrow if you're still stuck.

Marco
Post by Kryten
Thanks Marco,
That would be prompting the user to continue
between each element in the array is processed
by the pipeline.
That wouldn't really be practical, as sometimes
there could be be a thousand items being processed
and it would be inefficient, and annoying.
I was thinking more along the lines of scanning the
keyboard for physical keystrokes without actually
resorting to read-host? At a push it would be ok
to only listen for input as certain points in the
script but without actually requiring a prompt to
continue.
Any thoughts?
Thanks,
Stuart
stej
2010-03-09 06:28:59 UTC
Permalink
Zardos42 is right. However, it is better to combine it with
$host.ui.RawUI.KeyAvailable

1..100 | % {
if ($host.ui.RawUI.KeyAvailable -and
$host.UI.RawUI.ReadKey().Character -eq ' ') {
Read-Host -Prompt 'press enter to continue'
}
sleep -m 500 # give me chance to press the key ;)
$_
}
Post by Kryten
Thanks Marco,
That would be prompting the user to continue
between each element in the array is processed
by the pipeline.
That wouldn't really be practical, as sometimes
there could be be a thousand items being processed
and it would be inefficient, and annoying.
I was thinking more along the lines of scanning the
keyboard for physical keystrokes without actually
resorting to read-host? At a push it would be ok
to only listen for input as certain points in the
script but without actually requiring a prompt to
continue.
Any thoughts?
Thanks,
Stuart
Kryten
2010-03-09 07:49:01 UTC
Permalink
Thanks everyone - that's exactly
what I was looking for.

Much appreciated.

Thanks,
Stuart
Larry__Weiss
2010-03-09 17:57:36 UTC
Permalink
That's a great wrapper to demonstrate the technique. Thanks!

- Larry
Post by stej
Zardos42 is right. However, it is better to combine it with
$host.ui.RawUI.KeyAvailable
1..100 | % {
if ($host.ui.RawUI.KeyAvailable -and
$host.UI.RawUI.ReadKey().Character -eq ' ') {
Read-Host -Prompt 'press enter to continue'
}
sleep -m 500 # give me chance to press the key ;)
$_
}
Larry__Weiss
2010-03-09 20:10:03 UTC
Permalink
I made this variation that doesn't echo anything to the screen while the script
is running, yet allows the script to be paused and resumed.

$SPVOICE = new-object -com SAPI.SPVOICE;
$SPVOICE.Speak('press any key to pause') | Out-Null
1..100 | % {
if ($host.ui.RawUI.KeyAvailable)
{
$Host.UI.RawUI.FlushInputBuffer()
$SPVOICE.Speak('press any key to continue') | Out-Null
while (-not $host.ui.RawUI.KeyAvailable) {sleep -m 500}
$Host.UI.RawUI.FlushInputBuffer()
}
sleep -m 500
$SPVOICE.Speak('' + $_ + ' of 100') | Out-Null
}

I'm just now playing with the voice synthesis, What fun!

- Larry
Post by stej
Zardos42 is right. However, it is better to combine it with
$host.ui.RawUI.KeyAvailable
1..100 | % {
if ($host.ui.RawUI.KeyAvailable -and
$host.UI.RawUI.ReadKey().Character -eq ' ') {
Read-Host -Prompt 'press enter to continue'
}
sleep -m 500 # give me chance to press the key ;)
$_
}
Loading...