System shutdown () after a certain time in Windows

I run a command line application from a perl script (using system ()), which sometimes does not return, but rather it throws an exception that requires user input to interrupt the application. This script is used to automatically test the application that I run using the system () command. Since this is part of automatic testing, the sytem () command should return if an exception occurs, and consider the test to fail.

I want to write a piece of code that runs this application, and if an exception occurs, it should continue with the script, believing that this test will fail.

One way to do this is to run the application for a certain period of time, and if the system call does not return within this period of time, we must end the system () and continue using the script. ( How can I cancel a system command with an alarm in Perl? )

to achieve this:

my @output;
eval {
    local $SIG{ALRM} = sub { die "Timeout\n" };
    alarm 60;
    return = system("testapp.exe");
    alarm 0;
};
if ($@) {
    print "Test Failed";
} else {
    #compare the returned value with expected
}

but this code does not work on Windows, I did some research on this and found out that SIG does not work for Windows (Perl book programming). Can anyone suggest how I can achieve this in windows?

+5
source share
2 answers

Win32:: Process. , , . , , :

use Win32::Process;
use Win32;

sub ErrorReport{
    print Win32::FormatMessage( Win32::GetLastError() );
}

Win32::Process::Create($ProcessObj,
                       "C:\\path\\to\\testapp.exe",
                       "",
                       0,
                       NORMAL_PRIORITY_CLASS,
                       ".")|| die ErrorReport();

if($ProcessObj->Wait(60000)) # Timeout is in milliseconds
{
    # Wait succeeded (process completed within the timeout value)
}
else
{
    # Timeout expired. $! is set to WAIT_FAILED in this case
}

kill . , NORMAL_PRIORITY_CLASS - , ; . , DETACHED_PROCESS. , , .

+6

Proc:: Background, win32, linux, timeout_system( $seconds, $command, $arg, $arg, $arg )

+1

All Articles