Thanks again to all of you helping me so much.
Here I want to post the solution of my original problem.
#!/usr/bin/perl
use strict;
use threads;
use Thread::Queue;
use Win32::JobAdd;
## A shared var to communicate progess between work thread and TK
my $Q = new Thread::Queue;
my $job:shared = createJobObject( 'counter_and_calc_job' );
sub work{
my $pid = open PROC,
q[perl -le "$|=1; system 1, 'calc.exe'; print and select(undef,und
+ef,undef,0.1) for 1 .. 1000" |]
or die $!;
assignProcessToJobObject( $job, $pid );
while( <PROC> ) {
$Q->enqueue( $_ );
}
close PROC;
}
threads->new( \&work )->detach;
## For lowest memory consumption require (not use)
## Tk::* after you've started the work thread.
require Tk::ProgressBar;
my $mw = MainWindow->new;
my $pb = $mw->ProgressBar()->pack();
my $button = $mw->Button(-text => 'CANCEL',
-command => sub { closeHandle( $job ) } )->pa
+ck();
my $repeat;
$repeat = $mw->repeat( 100 => sub {
while( $Q->pending ) {
my $progress = $Q->dequeue;
return unless $progress;
$repeat->cancel if $progress == 100;
$pb->value( $progress )
}
});
$mw->MainLoop;
This code solves my two problems. Being able to read from a child process, but also being able to kill the whole process tree instead of only killing the child process and to have zombie processes.
If you press the "Cancel" Button then the calculator and the child process which counts from 1 to 1000 are killed. Even if you quit the gui with the cross in the right upper corner the grandchild process "calculator" is killed.
Without a job environment in a Win32 environment the grandchild calculator would stay as a zombie process.
Feedback to this code is welcome.
Cheers, Dirk
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.