Updating GUI from a callback using threads



Hello,

I need to develop quite simple GUI for a project at the University.

The main task of this GUI is to present results of a search to an
user. And since those searches (which are executed inside callbacks)
can take a few minutes to finish, I would like to update my GUI during
the search as an indication to a user, that program is working (and
not just standing there).

Below is a sample program that shows what I'm trying to achieve (when
the program exits, it prints error message "GLib-GObject-CRITICAL **:
g_object_unref: assertion `G_IS_OBJECT (object)' failed at test.pl
line 37." for each widget).

My problem is that I do not know how to use Gtk and threads in perl.
Any suggestions, documentation links ... are appreciated.

Thanks in advance.

Tadej


Code:
========================
#!/usr/bin/perl

# test.pl - playing with threads

use strict;
use warnings;
use utf8;
use threads;
use Time::HiRes qw{ sleep };
use Glib qw{ TRUE FALSE };
use Gtk2 qw{ -init };

# my global variables
my $window;
my $vbox;
my $progress;
my $button;


$window = Gtk2::Window->new( 'toplevel' );
$window->set_title( "Threaded program" );
$window->signal_connect( 'delete-event', sub { Gtk2->main_quit(); } );

$vbox = Gtk2::VBox->new( FALSE, 5 );
$window->add( $vbox );

$progress = Gtk2::ProgressBar->new();
$progress->set_pulse_step( 0.05 );
$vbox->pack_start( $progress, FALSE, TRUE, 0 );

$button = Gtk2::Button->new_with_label( "Test" );
$button->signal_connect( 'clicked', \&cb_butt, $progress );
$vbox->pack_start( $button, FALSE, TRUE, 0 );

$window->show_all();

Gtk2->main();

exit( 0 );



sub cb_butt {
        my $button = shift;

    my $progress = shift;
    my $thr;

        my $c = 0;



    $thr = threads->create( sub { sleep( 5 ); } );



    while( $thr->is_running() ) {

        local $| = 1;



        $progress->pulse();

                

                while( Gtk2->events_pending() ) {

                        Gtk2->main_iteration()

                }

                

        print( "debug: " . ++$c . "\n" );

        sleep( 0.1 );

    }



    $thr->join();
}

========================


-- 
Tadej BorovÅak
tadeboro gmail com


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