Re: Refreshing stuff in the background ( continued )




On Jun 5, 2006, at 8:59 PM, Daniel Kasak wrote:

Hi all.

Following on from my previous email, I've broken up all the code that
refreshes various notebook pages into separate subs. I've made an array of hashes describing each page, and with a coderef to the sub that does
the requerying for that page.

The main bit of code that triggers everything is:

sub refresh_pages {

    my $self = shift;

    # Refresh the active page immediately
    my $active_page =
$self->{form}->get_widget("TabForm_main_notebook")->get_current_page;

print "Refreshing page: $self->{refreshers}[$active_page]-> {name}\n";

    $self->{refreshers}[$active_page]->{refresh}();

    my $counter = 0;

    foreach my $refresher ( @{$self->{refreshers}} ) {
    if ( $counter ne $active_page ) {
        print "Refreshing page: $refresher->{name}\n";
        $refresher->{refresh}();
    }
    $counter ++;
    }

    Glib::Source->remove( $self->{refresh_timeout} );

}

 ... and I trigger this code with the line:

$self->{refresh_timeout} = Glib::Idle->add ( sub { $self- >refresh_pages } );

This code runs fine - ie it refreshes all the pages. However, even
though the window is 'responsive' ( ie redraws ), nothing actually
updates until ALL the pages are refreshed. So when I run the app from an
Eterm, I see the lines:

Refreshing page: xxx
Refreshing page: yyy

etc

and when the *final* one prints, *then* the window is actually updated
with the new data.

What am I doing wrong?

Screen updates don't happen immediately; they are queued in idles.

So, instead of

        install idle
    Main loop
        idle handler
            foreach @items
                refresh $_

break the list up into a chain of idle handlers:

        install idle
    Main loop
        idle handler
            refresh shift @items
            install idle if @items
    Main loop
        idle handler
            refresh shift @items
            install idle if @items
    ...

Yes, this can make you code rather more indirect and disconnected, but it gets the job done. If you structure things as a worker queue, it begins to fall together more naturally.

--
Meg: Brian! Chris picked his nose and keeps trying to touch me with his finger! Chris: What good is mining nose gold if I can't share it with the townspeople?
   -- Family Guy





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]