GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Wed Oct 22, 2014 10:22 pm

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: gthread
PostPosted: Thu Apr 10, 2008 4:56 am 
Offline
Familiar Face

Joined: Fri Sep 28, 2007 10:59 am
Posts: 46
In the following program , I have created a thread in the main func . when the window is displayed , it should place an image in each 1 sec , at the imagebox . it does it so good but when you click on the start button and the button_click function is run , the thread no longer work and dosent place an image in the imagebox .
[code=]
#include <gtk/gtk.h>


GtkWidget *image[6];
bool flag=true ;
bool setpic()
{
static int j=0 ;
if (j>5)
j=5;
gtk_image_set_from_file(GTK_IMAGE(image[j]),"images1.png");
j++ ;
return flag ;
}
void *thread_function(void *me)
{
g_timeout_add_seconds(1,GSourceFunc(setpic),NULL);

}
void click_button1()
{
flag=false ;
}

void click_button()
{
for (int i=0 ; i<10 ; i++)
g_print("%i",i);
}


int main(int argc , char *argv[])
{
if (!g_thread_supported())
g_thread_init(NULL);
gtk_init(&argc, &argv);
GtkWidget *window;
GtkWidget *vbox ;

GtkWidget *button=gtk_button_new_with_label("start");
GtkWidget *button1=gtk_button_new_with_label("set flag");
window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
for (int i=0 ; i<6 ; i++)
image[i]=gtk_image_new();
gtk_widget_set_size_request(window,300,300);
vbox=gtk_vbox_new(TRUE,0);
gtk_container_add(GTK_CONTAINER(window),vbox);

for (int i=0 ; i<6 ; i++)
gtk_box_pack_start_defaults(GTK_BOX(vbox),image[i]);
gtk_box_pack_start_defaults(GTK_BOX(vbox),button);
gtk_box_pack_start_defaults(GTK_BOX(vbox),button1);
///////
///////////
GThread * a_thread ;
a_thread=g_thread_create(thread_function , NULL , FALSE , NULL);
g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(click_button),NULL);
g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(click_button1),NULL);

gtk_widget_show_all(window);
gtk_main();
return 0;
}
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 11, 2008 1:41 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
There is a lot of information around about this, but you should check out this excellent tutorial: http://research.operationaldynamics.com ... eness.html

In short, only one thread can have access to GTK+ at a time. Therefore, you have to use calls such as gdk_threads_enter()/leave() to hand off and retrieve access to GTK+ between your threads.

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
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: No registered users and 2 guests


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