'alarm' doesn't yet work usefully, though this may change.
I'm sure there are better/more elegant solutions, but here is a sample script, using Win32::Process in stead, to run a (system) command, which times out.

Call like this perl alarmtest.pl 4 %SYSTEMROOT%\\system32\\ping.exe "ping localhost" where the paramaters are

a) Number of seconds till timeout
b) Full path to the executable
c) Command line to run.

# alarmtest.pl use strict; use Win32::Process; my @commands = @ARGV; timeout(@commands); print "End of test"; sub timeout { my ($time, $exe, $command) = @_; my ($obj, $exit, $counter); Win32::Process::Create($obj, $exe, $command, 1,NORMAL_PRIORITY_CLASS | CREATE_NEW_PROCESS_GROUP, '.') || die; { $obj->GetExitCode( $exit ) ; # the following constant isn't in # Win32::Process docs, but seems to # be true for my system at least. last unless $exit == 259; last if $counter++ >= ($time * 4); select(undef, undef, undef, 0.25); redo; }; if ($exit == 259) { print "Timed out!\n"; $obj->Kill(1); }; }

If you specify '$ENV{SYSTEMROOT}\system32\cmd.exe' as the application, then this doesn't seem to kill any child processes created by cmd.exe (I thought that adding the CREATE_NEW_PROCESS_GROUP constand should do this, but it's not very clearly documented in the Win32::Process docs. I think there's more details in Dave Roth's book: I'll check later on.

Update: Apparently if you are using Win32::Console, then using $CONSOLE->GenerateCtrlEvent(); on your Console object will send the Ctrl event to all the processes in that group.

Of course, if all you are doing is sleeping, then you could consider just sleeing less... e.g. a little at a time, checking the condition regularly.

For example, the following snippet will do pretty much the same as sleep 200 except that it will stop if the specified lockfile is ever created.

sleep_unless_lockfile (200, "$ENV{TEMP}\\lockfile"); sub sleep_unless_lockfile { my ($sleeptime, $lockfile)=@_; my $counter; { sleep 1; $counter++; print "$counter."; last if (-e $lockfile); last if $counter >= $sleeptime; redo; } }

Hope that's at all helpful.
Osfameron


In reply to Re: alarm implementation in Win32? by osfameron
in thread alarm implementation in Win32? by orbital

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.