how to use gtk2 threads

P

prameela

Iam using threads under gtk2.

In my program i wrote
1. use Gtk2 qw/-init -threads-init/ ;
2. use threads;
3.use threads::shared;

The Application is visible as empty when iam using (use Gtk2 qw/-init
-threads-init/ ) this statement.otherwise it is coming correctly.

what should i do to correct that and any module is required to run gtk2
threads.
 
Z

zentara

Iam using threads under gtk2.

In my program i wrote
1. use Gtk2 qw/-init -threads-init/ ;
2. use threads;
3.use threads::shared;

The Application is visible as empty when iam using (use Gtk2 qw/-init
-threads-init/ ) this statement.otherwise it is coming correctly.

what should i do to correct that and any module is required to run gtk2
threads.


Have you even bothered to look at the thread_usage.pl example that
comes with the Gtk2-perl module? It does almost exactly what you want,
and could be modified by you, to do your task.
I won't paste that example here, because you should be able to find it
yourself. It demonstrates the $thread->enter and $thread->leave methods.

Here is an alternative method, using shared variables.

If you can't understand these 2 examples, then don't use threads.


#!/usr/bin/perl
use warnings;
use strict;
use threads;
use threads::shared;
use Glib qw/TRUE FALSE/;
use Gtk2 qw/-init -threads-init/;

#setup shared hash to control thread
my %shash;
share(%shash); #will work for first level keys

$shash{'go'} = 0;
$shash{'data'} = '';
$shash{'work'} = '';
$shash{'die'} = 0;

#create 1 sleeping thread
my $thread = threads->new(\&work);

my $window = Gtk2::Window->new('toplevel');
$window ->signal_connect( 'destroy' => \&exit_thr );
$window->set_border_width(10);
$window->set_size_request(300,300);

my $vbox = Gtk2::VBox->new( FALSE, 6 );
$window->add($vbox);
$vbox->set_border_width(2);

my $hbox= Gtk2::HBox->new( FALSE, 6 );
my $hbox1 = Gtk2::HBox->new( FALSE, 6 );
$vbox->pack_end($hbox,FALSE,FALSE,0);
$vbox->pack_end (Gtk2::HSeparator->new, FALSE, FALSE, 0);
$vbox->pack_end($hbox1,FALSE,FALSE,0);
$hbox->set_border_width(2);
$vbox->pack_end (Gtk2::HSeparator->new, FALSE, FALSE, 0);

my $ebutton = Gtk2::Button->new_from_stock('gtk-quit');
$hbox->pack_end( $ebutton, FALSE, FALSE, 0 );
$ebutton->signal_connect( clicked => \&exit_thr );

my $pbar = Gtk2::progressBar->new();
$pbar->set_pulse_step(.1);
$hbox->pack_start($pbar,1,1,0);

######################################################

my $tbutton = Gtk2::Button->new_with_label('Run Thread');
$hbox1->pack_start($tbutton , 1, 1, 0 );
my $lconnect = $tbutton->signal_connect( clicked => sub{ launch() });
my $sconnect;

$window->show_all();
$pbar->hide; #needs to be called after show_all

Gtk2->main;

#####################################
sub delete_event {
Gtk2->main_quit;
return FALSE;
}
#######################################
sub launch{
$pbar->show;
$tbutton->set_label('Stop Thread');
$tbutton->signal_handler_block($lconnect);
$sconnect = $tbutton->signal_connect( clicked => sub{ stop() });
$shash{'go'} = 1;

Glib::Timeout->add (100,
sub {
if($shash{'go'} == 1){
$pbar->pulse;
return TRUE;
}else{
return FALSE;
}
}
);
}
##################################################
sub stop{
print "stopped\n";
$shash{'go'} = 0;
$pbar->hide;
$tbutton->set_label('Run Thread');
$tbutton->signal_handler_block ($sconnect);
$tbutton->signal_handler_unblock ($lconnect);
}
#########################################################
sub work{
$|++;
while(1){
if($shash{'die'} == 1){ return };

if ( $shash{'go'} == 1 ){

foreach my $num (1..100){
$shash{'data'} = $num;
print "$num\n";
select(undef,undef,undef, .5);

if($shash{'go'} == 0){last}
if($shash{'die'} == 1){ return };
}

$shash{'go'} = 0; #turn off self before returning
}else
{ sleep 1 }
}
}
#####################################################################
sub exit_thr{
$shash{'die'} = 1;
$thread->join;
Gtk2->main_quit;
return FALSE;
}
__END__
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top