in reply to Term::Twiddle on Win32 or how to use spinner
Here's a roll-your-own asynchronous spinner using fork under Win32. It's pretty crude as-is, but could be fairly easily wrapped up in a nicer interface.
Update: Added a 100ms sleep (select) to stop the spinner using half the CPU.
#! perl -sw use strict; use Win32::Mutex; $|++; my $name = 'Spin_control'; if ( my $pid = fork() ) { my $n=0; my $mutex =Win32::Mutex->new(0,$name) or die $^E; while($mutex->wait() != -1) { $mutex->release(); print chr(13) . 'Busy'; print substr( '-\|/', $n++ % 4,1) . chr(8); select(undef,undef,undef,0.1); ## Added. } } elsif ( $pid == 0 ) { my $mutex = Win32::Mutex->open($name); $mutex->wait(); print 'Not busy' . chr(13) for 1..1000; print $/; $mutex->release(); my $count; $count++ for 1..1_000_000; $mutex->wait(); print 'Counted to ', $count; } else { warn 'fork() failed'; }
Examine what is said, not who speaks.
The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.
|
|---|