in reply to Gtk2 app on Windows, driving a background process

Hi, I don't have any trouble with Glib. Here are few examples. If need be put your stdin reader in a thread. These work well on Linux, I hope they help you sort it out.
#!/usr/bin/perl use warnings; use strict; use Glib; my $main_loop = Glib::MainLoop->new; Glib::IO->add_watch (fileno 'STDIN', [qw/in/], \&watch_callback, 'STDI +N'); #just to show it's non blocking my $timer1 = Glib::Timeout->add (100, \&testcallback, undef, 1 ); $main_loop->run; sub watch_callback { # print "@_\n"; my ($fd, $condition, $fh) = @_; my $line = readline STDIN; print $line; if ($line eq "q\n"){exit} #always return TRUE to continue the callback return 1; } sub testcallback{ print "\t\t\t".time."\n"; return 1; } __END__
or
#!/usr/bin/perl -w use strict; use Gtk2 -init; use Glib qw(TRUE FALSE); Glib::IO->add_watch (fileno 'STDIN', [qw/in/], \&watch_callback, 'STDI +N'); Gtk2->main; sub watch_callback { my ($fd, $condition, $fh) = @_; if(sysread(STDIN, my $buf, 1024)){ print "$buf\n" } #always return TRUE to continue the callback return TRUE; }
or last resort put your stdin reading in a thread. Remember Gtk2 runs on Glib so this will work in a Gtk2 Mainloop as well. I use a Glib mainloop to show the simplicity.
#!/usr/bin/perl use warnings; use strict; use Glib; use Term::ReadKey; use threads; $|++; ReadMode('cbreak'); # works non-blocking if read stdin is in a thread my $count = 0; my $thr = threads->new(\&read_in)->detach; my $main_loop = Glib::MainLoop->new; my $timer = Glib::Timeout->add (1000, \&timer_callback, undef, 1 ); # can also have filehandle watches #my $watcher; #$watcher = Glib::IO->add_watch( fileno( $pty ), ['in', 'hup'], \&call +back); # must be done after main_loop is running #Glib::Idle->add( sub{}); #print "$ps\n"; my $timer1 = Glib::Timeout->add (10, \&testcallback, undef, 1 ); $main_loop->run; ReadMode('normal'); # restore normal tty settings sub testcallback{ my $ps = `ps auxww`; print "$ps\n"; return 0; #only run once } sub timer_callback{ #do stuff $count++; print "\n$count\n"; return 1; } sub read_in{ while(1){ my $char; if (defined ($char = ReadKey(0)) ) { print "\t\t$char->", ord($char),"\n"; #process key presses here if($char eq 'q'){exit} #if(length $char){exit} # panic button on any key :-) if($char eq 'p'){ Glib::Idle->add( sub{ my $ps = `ps auxww`; print "$ps\n"; return 0; # run only once } ); } } } } __END__

I'm not really a human, but I play one on earth. ..... an animated JAPH

Replies are listed 'Best First'.
Re^2: Gtk2 app on Windows, driving a background process
by Anonymous Monk on May 31, 2017 at 14:00 UTC

    Have you actually tried these on Windows?

    In the first two programs the GUI freezes as soon as I begin to type into STDIN. As for threads, the official FAQ recommends that secondary (worker or other) threads should be created before any GUI widgets, and that the GUI should be touched from only one (the main) thread. In any case, threads would complicate my (real) program to an unacceptable degree.

      In the first two programs the GUI freezes as soon as I begin to type into STDIN

      For me on Windows 7, I think the first one might be working fine (as a standalone script, at least). I find that output ceases as soon as I start to enter something into STDIN, but then resumes as soon as I hit 'Enter'.
      Output for me on perl 5.24.0 is:
      C:\_32\pscrpt\gtk2>perl timer1.pl 1496276122 1496276122 1496276122 1496276122 1496276122 1496276122 my input my input 1496276130 1496276131 1496276131 1496276131 1496276131 1496276131 1496276131 1496276131 1496276131 my next input my next input 1496276143 1496276144 1496276144 1496276144 1496276144 1496276144 1496276144 1496276144 1496276144 1496276144 1496276145 ....
      However,YMMV.
      Even if that is correct behaviour, the script is definitely buggy on perl-5.16.0.
      I haven't tested versions in between.

      Cheers,
      Rob