Widgets and GThreads.
- From: "Claudio Saavedra V." <csaavedra alumnos utalca cl>
- To: gtk-list gnome org, gtk-app-devel-list gnome org
- Cc:
- Subject: Widgets and GThreads.
- Date: Sun, 6 Jun 2004 18:19:19 -0400
Hi
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:
-- code --
#include <stdio.h>
#include <gtk/gtk.h>
#include <stdlib.h>
#include "gthread.h"
static void
print_something (GtkWidget *widget,
gpointer data)
{
g_print ("this is the window thread\n");
}
static void
destroy (GtkWidget *widget,
gpointer data)
{
gtk_main_quit ();
}
void *
thread_pipe_socket (GtkButton *button)
{
GError *error = G_IO_ERROR_NONE;
GIOStatus *status;
gint len;
GString *buffer = g_string_new ("");
GIOChannel *channel;
FILE *foo;
gchar **new_label;
/* numbers is a program that generates numbers, sends it to the stdout
and sleep 1 second. */
foo = popen ("./numbers", "r");
channel = g_io_channel_unix_new (fileno (foo));
if(error){
g_print("(%i) %s\n", error->code, error->message);
return;
}
if (channel != NULL) {
g_print ("Socket ready\n");
while (g_io_channel_read_line_string
(channel, buffer, &len, &error) == G_IO_STATUS_NORMAL){
g_print ("%s", buffer->str);
new_label = g_strsplit (buffer->str, "\n", -1);
gtk_button_set_label (button, new_label[0]);
g_strfreev (new_label [0]);
}
}
}
int
main (int argc,
char *argv[])
{
GtkWidget *window;
GtkWidget *button;
pthread_t thread;
g_thread_init (NULL);
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
g_signal_connect (G_OBJECT (window), "delete_event",
G_CALLBACK (destroy), NULL);
g_signal_connect (G_OBJECT (window), "destroy",
G_CALLBACK (destroy), NULL);
button = gtk_button_new_with_label ("This is one thread");
g_signal_connect (G_OBJECT (button), "clicked",
G_CALLBACK (print_something), NULL);
gtk_container_add (GTK_CONTAINER (window), button);
gtk_widget_show_all (window);
pthread_create (&thread, NULL, thread_pipe_socket, button);
gdk_threads_enter ();
gtk_main ();
gdk_threads_leave ();
return 0;
}
-- 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.
Thanks.
Claudio
Please, don't send me any attachment in Microsoft (.DOC, .PPT) format.
Read http://www.fsf.org/philosophy/no-word-attachments.html
Preferable attachments: .PDF, .HTML, .TXT
Please, consider adding this text to Your email signature.
--
Claudio Saavedra Valdés
http://csv.no-ip.org
mailto:csaavedra_at_alumnos_dot_utalca_dot_cl
Ingeniería Civil en Computación
UTALCA - Chile.
-------------------------------------------------
Este correo fue enviado por http://alumnos.utalca.cl
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]