[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: Widgets and GThreads.



On Sunday 06 June 2004 23:19, Claudio Saavedra V. wrote:

> I am writing a GTK+-2.0 code that is supposed to change the label of a
> GtkButton constantly in a thread diferent than the main, while the button
> is usefull.
>
> The next is the code i have until now:
>
>    (snip code)
>
> This works fine, the label is changed when a new number gets in the
> channel.
>
> However, if i click in the button repeatedly, the button looses the label.
> I guess that this is because the widget is being accesed in a wrong way
> (not locking it) by gtk_button_set_label.
>
> How can i lock a widget? i have seen some examples for locking variables
> but none of them seems to work fine with widgets that are modified by the
> main loop.
>
> Is that really the problem? or am i missing something important?
>
> I am very lost so please, any hint will help me.

Why not use g_spawn_async_with_pipes() in connection with a GIOChannel and 
g_io_add_watch() instead of a separate thread?  (or is the numbers program 
just a placeholder for a calculation?)

If you use threads, you could try something like this:

  struct foo_helper 
  {
    GtkButton *button;
    gchar        *newtxt;
  };

  static gboolean
  button_change_text_main (struct foo_helper *h)
  {
     gtk_button_set_text (h->button, h->newtxt);
     g_free (h->newtxt);
     g_free (h);
     return FALSE; /* only once */
  }

  void *
  thread_pipe_socket (GtkButton *button)
  {
       ....
      struct foo_helper *h;

      h = g_new0 (struct foo_helper, 1);
      h->button = button;
      h->newtxt = g_strdup (new_label[0]);

      g_idle_add ((GSourceFunc) button_change_text_main, h);

       ....
  }

That way button_change_text_main() will be run from within the main thread as 
soon as Gtk has nothing else to do, and you don't have to worry about locking 
gdk/gtk stuff etc.

Cheers
 -Tim



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]