Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 239693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:36:21+00:00 2026-05-11T20:36:21+00:00

I have a Perl program that has a GTK2 GUI (via the Gtk2 package).

  • 0

I have a Perl program that has a GTK2 GUI (via the Gtk2 package). This program also opens a network socket (actually via LWP) in another thread, and continuously makes a request for a certain URL, waiting for an event to happen.

If an event occurs, then its data must be processed and interpreted, and an appropriate callback function used to update the GUI. This is where my program falls down.

Main program:

# Attach to the "message received" event
Foo::hook('msgRx', \&updateMsg);

# ...

Gtk2->main();

sub updateMsg {
    my ($msg) = @_;
    print "New message: $msg\n";
    # append to a GTK TextView -- code is also used elsewhere and works fine
    appendMsg($msg); 
}

And in the module:

# ...
my %hooks = ();
my $ev_pid = undef;

sub hook($&) {
    my ($name, $sub) = @_;
    $hooks{$name} = $sub;
}

sub call_hook {
    my ($name, @args) = @_;
    print ">>> CALLING HOOK $name\n";
    return $hooks{$name}->(@args) if (defined($hooks{$name}));
}

sub eventThread {
    while (1) {
        my $res = $browser->post("$baseurl/events", ['id' => $convid]);
        my $content = $res->content;

        last if ($content eq 'null');

        my $events = from_json($content);
        foreach (@$events) {
            my $ev_type = shift @$_;
            my @ev_args = @$_;
            print "Event type: $ev_type\n";
            print Data::Dumper->Dump([@ev_args]);
            handleEvent($ev_type, @ev_args);
        }
    }
}

sub doConnect() {
    # ...
    $ev_pid = fork;
    if (!defined $ev_pid) {
        print "ERROR forking\n";
        disconnect();
        return;
    }
    if (!$ev_pid) {
        eventThread;
        exit;
    }
}

Now the console output from these is what I expect:

>> Starting...
[["connected"]]
Event type: connected
>>> CALLING HOOK start
[["waiting"]]
Event type: waiting
>>> CALLING HOOK waiting
[["gotMessage", "77564"]]
Event type: gotMessage
$VAR1 = '77564';
>>> CALLING HOOK msgRx
New message: 77564
[["idle"]]
Event type: idle
>>> CALLING HOOK typing
[["gotMessage", "816523"]]
Event type: gotMessage
$VAR1 = '816523';
>>> CALLING HOOK msgRx
New message: 816523
>> User ending connection
null
>>> CALLING HOOK end

However, the GUI TextView does not update. I can only presume that this is because the callback is actually happening in another thread, which has duplicates of the objects.

Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T20:36:21+00:00Added an answer on May 11, 2026 at 8:36 pm

    If you are forking, you need to implement some kind of IPC mechanism between your processes. In this case, a simple socket pair that connects the parent and child processes should suffice. See “Bidirectional Communication with Yourself” in perlipc on how to do this.

    If the child process has new data available, just write it to the socket. In the main process, install a listener for the socket (I assume Gtk2 uses Glib under the hood, if so then Glib::IO::add_watch is what you need). If new data is available, the handler will be called and can update your GUI.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Perl program, that needs to use packages (that I also write).
I've developed a Perl script that has a confidential business logic. I have to
We have a Perl program to validate XML which is invoked from a Java
I have a bunch of PDF files and my Perl program needs to do
I have a program in Perl I'm working on where I would need multiple
I have a C program with an embedded Perl interpreter. I want to be
I'm writing a combination Java/Perl program that parses XML files into an Oracle SQL
I have a program that downloads basic historical stock data from yahoo and puts
I have a Java program that runs a number of other programs. Once the
I am new to perl scripting. I have a Perl program I would like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.