sub button_press {
my $sthread = threads->new(sub { worker_thread() });
$sthread->detach();
# Here we "return immediately" :)
return;
}
####
#!/usr/bin/perl
use warnings;
use strict;
use Tk;
my $mw = MainWindow->new(-background => 'gray50');
my $text = $mw->Scrolled('Text')->pack();
my $pid;
my $startb = $mw->Button( -text => 'Start',
-command=> \&work,
)->pack();
my $count = 0;
my $label = $mw->Label(-textvariable=>\$count)->pack();
my $testtimer = $mw->repeat(500,
sub { $count++} );
my $stopb = $mw->Button( -text => 'Exit',
-command=>sub{
kill 9,$pid;
exit;
},
)->pack();
MainLoop;
#####################################
sub work{
$startb->configure(-state=>'disabled');
use Fcntl;
my $flags;
#long 10 second delay between outputs
$pid = open (my $fh, "top -b -d 10 |" ) or warn "$!\n";
fcntl($fh, F_SETFL, O_NONBLOCK) || die "$!\n"; # Set the non-block flags
my $repeater;
$repeater = $mw->repeat(10,
sub {
if(my $bytes = sysread( $fh, my $buf, 1024)){;
$text->insert('end',$buf);
$text->see('end');
}
}
);
}
####
#!/usr/bin/perl
use warnings;
use strict;
use IPC::Open3;
use Tk;
$|=1;
my $pid=open3(\*IN,\*OUT,0,'/bin/bash');
my $mw=new MainWindow;
$mw->geometry("600x400");
my $t=$mw->Scrolled('Text',-width => 80,
-height => 80,
)->pack;
&refresh;
$mw->fileevent(\*OUT,'readable',\&write_t);
my $id = Tk::After->new($mw,2000,'repeat',\&refresh);
MainLoop;
sub refresh{
print IN "top b n 1";
print IN "\n"; #absolutely needed and on separate line
}
sub write_t {
my $str= ;
$t->insert("1.0",$str);
# $t->see("0.0");
}
__END__