user46573544 user46573544 - 1 month ago 30
C Question

How to use g_timeout_add?

I am making a program in C with GTK and Glade for a serial communication. I am having problem using g_timeout_add. For example I have a function

serial_data()
which contain my serial data and I have a button handler
on_update_button_clicked()
. So till now I have done that if
update button
is clicked,
gtk_timeout
should run. But it running just for one time.

on_update_button_clicked(GtkButton *Update_Button)
{
//2nd argument is serial_data function which contain actual data
g_timeout_add(250,serial_data,NULL);
}


where I am missing the point?

I have another button
stop button
. So i want that
timeout
should stop when
stop button handler
is clicked. How to do that.??

One more question to ask, I want to count the number of times
timeout
is running like a counter. So that I can display the numbers of counter. How is this possible.?
Please help thanks.

jku jku
Answer

g_timeout_add() returns an event source id that you should store. You can use g_source_remove() with that id in your stop button handler to stop the timeout.