in reply to Re^3: Weird Output with Threads and NCurses
in thread Weird Output with Threads and NCurses
The calls are documented in POD at the end of Async::Tiny. Just run "perldoc Async/Tiny.pm" to see the formatted version.
The first argument to addRepeatCallback is the interval in seconds, and the second is the callback subref.
Any extra arguments are provided to the callback sub when it is called.
In this case they are the row number at which to display the count, and an array ref which holds as its first
element the actual count. This array ref holds the "state" for this particular "thread" of repeated callbacks,
so I can have two (or more, if desired) independent counts running at the "same" time using the same counter sub.
( see the "counter" sub for how those two extra arguments are handled.)
"simultaneously" ? yes and no :)
My example program shows two counters incrementing "simultaneously" at two different rates.
Here's another example program (a newer version of the "bars.pl" in the POD)
where each row sweeps at its own rate independent of the other rows (and "simultaneously").
(This is one of my test programs for the timerqueue.)
#!/usr/bin/perl # # bars.pl - multiple independent timers using Async::Tiny Ppoll use Curses; use Term::ReadKey; use Async::Tiny; use strict; use warnings; my ($width, $height) = GetTerminalSize; my @lines = ( '-' x $width ) x $height; my $endcode; my $t = Async::Tiny->new; $t->addDelayCallback( 100, sub { $endcode = 'endrepeat' } ); for my $row (0 .. $#lines) { $t->addRepeatCallback( (3 + rand 20) / 50 , sub { s/-/#/ or s/#*\K#/=/ or tr/=/-/ for $lines[$row]; addstr $row, 0, $lines[$row]; refresh; $endcode; } ); } initscr; curs_set 0; addstr 0, 0, '-' x $width x $height; $t->eventloop; endwin;
I'll see if I can do a simple game with action, interaction, and score keeping as "proof of concept" sometime soon.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Weird Output with Threads and NCurses
by Anonymous Monk on Jul 19, 2016 at 18:12 UTC | |
by Anonymous Monk on Jul 20, 2016 at 04:59 UTC |