> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] Behalf Of
> [EMAIL PROTECTED]
> Sent: Friday, October 07, 2005 6:02 AM
> To: [email protected]
> Subject: RE: how to interact with an endless loop
> 
> Eugene,
> 
> I had a loop that took hours to complete and it ran multiple 
> iterations
> over several days. I could have pressed Control-C or Control-Break but
> that would have killed it instantly with completing the current
> iteration. What I wanted was to complete the current 
> iteration and exit
> cleanly.
> 
> I solved it with the code below. While your loop is running the system
> stores any key presses in a buffer. When your loop gets to the code
> below it reads all the characters in the buffer and looks for your
> terminate sequence. If it finds it any where in the buffer it exits
> cleanly. Currently it is looking for the string OK (case insensitive).
> If you want it to be case sensitive use the following instead:
> 
>    ($keyboard_buffer_contents =~ /OK/)
> 
> 
> use strict;
> 
> use Term::ReadKey;
> 
> my $keyboard_buffer_contents;
> 
> while (1) {    # beginning of your process loop
> 
>    # your process
> 
>    ReadMode ('cbreak');
> 
>    my $char;
> 
>    while (defined($char = ReadKey(-1))) {
> 
>       $keyboard_buffer_contents .= $char;
> 
>    }
> 
>    ReadMode ('normal');
> 
>    if (
> 
>         (defined ($keyboard_buffer_contents)) and
>         ($keyboard_buffer_contents =~ /OK/i)
> 
>       ) {
> 
>       exit;
>   }
> 
> }   # end of your process loop
> 

Thank you all for your replies to the original post. So far I have tried out
both "keyboard buffer" method (Keith) and SIGINT method (pDale). Both suit
my need and simple enough.

I thought I could use "open" or "pipe" to do the trick easier (still wonder
if I can), I am glad to know there are many other tricks too. Once again
thank you all.

-Eugene
_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to