GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

gthread

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
alen121
Familiar Face


Joined: 28 Sep 2007
Posts: 28

PostPosted: Thu Apr 10, 2008 4:56 am    Post subject: gthread Reply with quote

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: (Plaintext)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

#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;
}
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 386
Location: Fairfax, Virginia

PostPosted: Fri Apr 11, 2008 1:41 pm    Post subject: Reply with quote

There is a lot of information around about this, but you should check out this excellent tutorial: http://research.operationaldynamics.com/blogs/andrew/software/gnome-desktop/gtk-thread-awareness.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.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP