On Wed, 3 Jan 2001, Robert Sherman wrote:

> sub Loop_Click {
>       while ($i<20) {
>               sleep 1;
>               if ($i==10){
>                       &Show_Win2($i);
>                       }
>               $i++;
>               }
> }

One way to do this is to use a timer:


# Warning: Untested code

my $loop_counter;
my $LoopTimer = Window->AddTimer("LoopTimer", 0);

sub Loop_Click {
   $loop_counter = 0;
   $LoopTimer->Interval( 1000 );
      # Use a short interval if all you want is event procesing.
      # Here the 1000ms interval substitutes for your sleep 1.
}

sub LoopTimer_Timer {
   &Show_Win2( $loop_counter )  if $loop_counter == 10;
   ++$loop_counter;
   $LoopTimer->Kill() if $loop_counter >= 20;
}


If you have a lot of loop state and you don't want it all at file level
you can also use a closure.  This has a side effect of making the timer
generic.


# Warning: Untested code

my $loop_body;
my $LoopTimer = Window->AddTimer("LoopTimer", 0);

sub Loop_Click {
   my $loop_counter = 0;

   $loop_body = sub {
      &Show_Win2( $loop_counter )  if $loop_counter == 10;
      ++$loop_counter;
      $LoopTimer->Kill() if $loop_counter >= 20;
   }

   $LoopTimer->Interval( 1000 );
}

sub LoopTimer_Timer {
    $loop_body->();
}


This could be done very nicely with perl-level threads. (i.e. one at a
time but with multiple call stacks where one could pass control to
another)

   - Eric B.

--
"An intelligent carrot!  It boggles the mind."


Reply via email to