Re: hello & threads
- From: "david::" <voiser gmail com>
- To: gtk-app-devel-list gnome org
- Subject: Re: hello & threads
- Date: Sat, 15 Jan 2005 14:40:15 +0100
Perfect, it works perfect with g_timeout_add.
thanks for your time, Tim
On Sat, 15 Jan 2005 12:55:03 +0000, Tim Müller <zen18864 zen co uk> wrote:
On Saturday 15 January 2005 12:27, david:: wrote:
well i have a problem with an application i'm writing. I have 2
threads. The first one creates an empty window with a button and calls
gtk_main(). The second one does this:
void secondThread(){
char str[100];
int i=0;
while(TRUE) {
// here i create a string
sprintf(str, "%d\n", i++);
// and here I change the button label
if (window != NULL)
gtk_button_set_label(GTK_BUTTON(button), str);
// once a second
g_usleep(1000000);
}
}
well the thread is quite simple. But when i launch the program the
button changes its label 3 or 4 times and then becomes blank.
I well, i have been using gtk for a time and i have never had this
problem. can you tell me what i am doing wrong??
Don't use Gtk or Gdk functions from any thread that's not the main GUI thread.
If you do, you'll have to use locks (see http://www.gtk.org/faq/ for
details), but I really don't recommend this.
In your case, you could do somthing like:
static gboolean
change_label (gchar *str)
{
if (window != NULL)
gtk_button_set_label (GTK_BUTTON (button), str);
g_free (str);
return FALSE; /* only once */
}
.....
// change label here
g_idle_add ((GSourceFunc) change_label, g_strdup (str));
....
change_label will be called as soon as possible from the context of the main
thread.
I assume this is just a simple example. Because if not, you don't need threads
at all, just do something like:
static gboolean
update_button (gpointer the_button)
{
static gint i; /* 0 */
gchar *str;
str = g_strdup_printf ("%d", i++);
gtk_button_set_label (GTK_BUTTON (the_button), str);
g_free (str);
return TRUE; /* call again */
}
and then .....
.... set up window etc....
button = gtk_button_new ( .... );
g_timeout_add (1 * 1000, update_button, button);
gtk_main ();
That way the function update_button() will be called once per second (until
you return FALSE).
Cheers
-Tim
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]