w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Executing Gtk.ListBox.insert and Gtk.ListBox.append in another thread causes Vala program to segfault

All calls to GDK and GTK+ should be made from the main thread. This is quite easy to do with Vala by using GLib.Idle.add, especially if you keep in mind that Vala supports closures:

// Do stuff in your thread (CPU-intensive processing, I/O, etc.)
var value = blocking_operation ();

GLib.Idle.add (() => {
    // You can access local variables, like value, here.
    widget.set_something (value);

    // Returning false means the idle callback will be removed
    // after it is called (instead of being called repeatedly).
    return false;

Basically, what you want to do is run your tasks in the other thread(s), and when they are complete switch back to the main thread (using an idle callback) to modify the GTK+ stuff.

That said, I think you can safely modify widgets which aren't part of any hierarchy GTK+ is drawing yet. So you could instantiate a container, add a bunch of children, etc., in your thread then just add it to your window in the main thread. Just don't change anything which GTK+ may currently be trying to draw.

© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.