var
P: TProcess;
begin
...
P.Execute;
if not P.WaitOnExit(5000) then P.Terminate(0);
end.
use Coro;
my $p = Coro->new( sub{ ... } );
$p->ready;
my $start = time;
for (1..100) {
cede;
if ( (time - $start) > 5 ) {
$p->cancel;
last;
}
}
CPAN module Coro provides threading support. Here we create a thread $p to do some work, and in the main thread we cede control to it within a loop doing other work. When the current time minus start time exceed 5 seconds, we cancel thread $p.