views:

113

answers:

2

I have an application that waits for the user to press a key and then executes a long running method that periodically updates the GUI.

sub keypress{
  my $self = shift;
  my $event = shift;

  if ($event->GetKeyCode() == WXK_F12){
      $self->doStuff();
  }else{
   $event -> Skip(0);
  }
}

I would like to have the application ignore any keypresses that occur while the doStuff method is doing it's thing.

I tried setting a flag and wrapping the method call in a additional if statement but the kepress method is not entered until doStuff finishes.

I'm kind of new to wxwidgets and feel like I don't know what I don't know. Any suggestions on what to explore next would be greatly appreciated.

+3  A: 

I am not sure this is the best way to ignore events in the WxWidgets framework (since you probably don't really want to freeze the GUI every time you perform an operation!), but setting a flag should be fine. Just make sure you use a guard so that your flag is unset even if the stack unwinds due to an exception:

use Guard;
my $flag;

sub keypress{
    my $self = shift;
    my $event = shift;

    if ($event->GetKeyCode() == WXK_F12 && !$flag){
        $flag = 1;
        scope_guard { $flag = 0 };

        $self->doStuff();
    }
    else {
        $event->Skip(0);
    }
}

You probably really want to show a progress bar instead, though, or perhaps only disable a single action. Nobody wants their GUI to freeze and not provide any feedback!

jrockway
+1  A: 

I would use some sort of global or singleton to ensure the method only runs when it's not already running. Then it will just exit on subsequent keystrokes.

Swish