Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re^3: xcb Unknown sequence number while processing queue in Perl/Gtk2

by Anonymous Monk
on Jan 09, 2015 at 04:22 UTC ( #1112703=note: print w/replies, xml ) Need Help??

in reply to Re^2: xcb Unknown sequence number while processing queue in Perl/Gtk2
in thread xcb Unknown sequence number while processing queue in Perl/Gtk2

Ok, here is how to do that , the threading communication setup
#!/usr/bin/perl -- use strict; use warnings; use threads; use Thread::Queue; my $qin = Thread::Queue->new(); my $qout = Thread::Queue->new(); my $guithread = threads->create( sub { require MyApp::GUI; ## MyApp/ MyApp::GUI::WorkQ(@_); ## uses timer to check on queue }, $qin, $qout, ); my $dbithread = threads->create( sub { require MyApp::DBFetch; ## MyApp/ MyApp::DBFetch::WorkQ(@_); }, $qin, $qout, ); $guithread->join; ## wait for gui to finish __END__

Then the background worker thread waits for instructions (jobs to do from $qin), and sends messages back (on $qout)

sub MyApp::DBFetch::WorkQ { my( $qin, $qout ) = @_; my %stash; while( defined( my $argRef = $qin->dequeue ) ) { my $action = delete $argRef->{action}; if( $action eq 'connect' ){ $stash{dbi} = DBI->...; $qout->enqueue( { status => 'you are connected' } ); } } }

Then the gui thread ... uses buttons to send jobs to worker thread, and sets up a timer to check for return messages from the worker thread

sub MyApp::GUI::WorkQ { my( $qin, $qout ) = @_; .... Glib::Timeout->add( 50, sub { CheckQ( $qout, $statuswindow} } ); Gtk2->main; } sub OnConnectButton { my( $qin, $passwindow ) = @_; my $password = $passwindow->get_text; ... $qin->enqueue( { action => 'connect', password => $password, ... } + ); } sub CheckQ { my( $qout, $statuswindow ) = @_; if( defined( my $ref = $qout->popnow ) ) { my $msg = $ref->{status}; $statuswindow->set_text( $msg ); } return 1; ## don't cancel timeout, repeat it }

Does that make sense? There are other ways to do threading, but its more complicated :) see What is the usage of "use Gtk2 -init -threads-init"? and and don't ask me about it :)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1112703]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2018-06-21 01:58 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (117 votes). Check out past polls.