Search code examples
perlwxwidgetswxperl

How to cancel a long time operation in WxPerl


I have a program like this

...
$self->{bn1}=Wx::Button->new( $tb1, -1, 'Start');
EVT_BUTTON( $self, $self->{bn1}, \&on_click_start );
...
...

sub on_click_start
{
    my( $this, $event ) = @_;
    $this->{bn1}->SetLabel("Cancel");
    $event->Skip;
    for (...) {
        long_time_operation();
        last  if ( Cancel_clicked );
    }
}
...

My problem is when I click the Start button, on_click_start() will be called, and I want change the label of Start button to Cancel, that allow I to click the button to break out the long_time_operation() loop.

How do I make right code for it?


Solution

  • The only real solution is to use multiple threads and perform the long operation in a background thread. If you want to keep your code simple, you can use wxYield() to handle the events from inside this event handler, but be aware that this may (and will) result in difficult to debug problems due to reentrancy, so at the very least you need to disable the rest of your UI if you're doing it like this.