GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Tue Aug 30, 2016 9:07 am

All times are UTC

Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: gthread: UI update during intensive IO function
PostPosted: Tue Aug 05, 2014 11:30 am 
Familiar Face

Joined: Mon Aug 04, 2014 7:31 am
Posts: 5
static gboolean
fill (gpointer data)
    gtk_progress_bar_pulse (GTK_PROGRESS_BAR (data));

    return TRUE;

static void
do_something (struct var *vars)
    GtkWidget *dialog, *contentArea;
    gint result;
    dialog = gtk_dialog_new_with_buttons ("pbar",
                     GTK_WINDOW (vars->mainwin),
                     _("Close"), GTK_RESPONSE_REJECT,

    contentArea = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
    vars->pBar = gtk_progress_bar_new ();

    gtk_progress_bar_pulse (GTK_PROGRESS_BAR (vars->pBar));

    guint id = g_timeout_add (100, fill, GTK_PROGRESS_BAR (vars->pBar));

    gtk_container_add (GTK_CONTAINER (contentArea), vars->pBar);
    gtk_widget_show_all (dialog);

    GThread *n = g_thread_new(NULL, do_heavy, vars);

    result = gtk_dialog_run (GTK_DIALOG(dialog));
    switch (result)
            gtk_widget_destroy (dialog);

When the user click on the Cancel button the progess bar freeze until the thread finishes.
How can i prevent this behavior and updating the UI also when the Cancel button is clciked?

 Post subject: Re: gthread: UI update during intensive IO function
PostPosted: Sat Aug 09, 2014 6:31 am 
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 1048
Location: UK

In your section of code you have not told the thread to end. Therefore it will run until it has terminated.

The reason the application UI freezes is that you have called g_thread_join () before the thread has terminated. What g_thread_join () does is wait for the thread to terminate (this will put the calling thread in a waiting state, in this case your UI thread). Once the thread we are waiting for has terminated then the calling thread wakes up and g_thread_join () will return the exit value of the the thread that we were waiting for.

What you need to do is find some communication method to tell the worker thread to exit early as you may need to do some clean-up or other work in the thread before it terminates. This could be as simple as checking a value periodically which tells the thread that it should keep running normally, but remember to use a Mutex.


Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group