GTK gossip: timeout and idle

Source: Internet
Author: User
Tags gtk
You can use g_timeout_add () or g_timeout_add_full () and g_timeout_add () to set the values of g_timeout_add () as follows:

Guint g_timeout_add (guint interval,


Gsourcefunc
Function,


Gpointer
Data );


The first parameter is the time interval, in milliseconds, and the second parameter is the time-to-call function, the third response is the information sent to the callback function

Internal
Generation and stop of Signal


For example, you can use g_timeout_add () to change the values as follows, and the results of the rows are the same:

  • G_timeout_demo.c
# Include <GTK/GTK. h>

Gboolean timeout_callback (gtkbutton * button ){
Static Gint COUNT = 0;
If (count <5 ){
G_signal_emit_by_name (button, "clicked ");
Count ++;
Return true;
}
Else {
Return false;
}
}

// Callback function
Void button_clicked (gtkwidget * button, gpointer data ){
G_print ("Press": % s/n ", (char *) data );
}

Int main (INT argc, char * argv []) {
Gtkwidget * window;
Gtkwidget * button;

Gtk_init (& argc, & argv );
Window = gtk_window_new (gtk_window_toplevel );
Gtk_window_set_title (gtk_window (window), "haha! GTK +! ");

Button = gtk_button_new_with_label ("press me ");
Gtk_container_add (gtk_container (window), button );

G_signal_connect (gtk_object (window), "Destroy ",
G_callback (gtk_main_quit), null );
G_signal_connect (gtk_object (button), "clicked ",
G_callback (button_clicked), "haha! Press confirm! ");

G_timeout_add (1000, (gsourcefunc) timeout_callback, button );

Gtk_widget_show (window );
Gtk_widget_show (button );


Gtk_main ();

Return 0;
}


In the callback method, if the return value is true, the next time is used,

The next time the timer is scheduled, it starts after the callback function is completed,

When the value is set to false, the timer ends and auto-renewal is triggered. If you use

G_timeout_add_full ():

Guint g_timeout_add_full (Gint priority,


Guint interval,



Gsourcefunc function,



Gpointer data,



Gdestroypolicy notify );

The first Shard is the primary row permission of the hour to the hour. You can set the primary privilege as follows:

  • G_priority_high
  • G_priority_default (reset)
  • G_priority_high_idle

  • G_priority_default_idle

  • G_priority_low


The last response is a function to be written when the timer is removed.

For a specific operation, you can use the g_idle_add () or g_idle_add_full () function, when the program does nothing (for example, there is no user operation, there is no program execution time to run), you can also do something, if g_idle_add () is used ():

Guint g_idle_add (gsourcefunc function,


Gpointer data );

The first response is the callback function, and the second response is the information sent to the callback function. For example, in the following example, when the user does not do anything, the interface returns the specified idle function of the row, and the callback function of the row according to the specified value when the row is pressed:

  • G_idle_demo.c
# Include <GTK/GTK. h>

Gboolean idle_callback (gpointer data ){
G_print ("% S .. XD/N ", data );
Return true;
}

Void button_pressed (gtkbutton * button, gpointer data ){
Int I;
For (I = 0; I <10; I ++ ){
G_print ("% s.../N", data );
Sleep (1 );
}
}

Int main (INT argc, char * argv []) {
Gtkwidget * window;
Gtkwidget * button;

Gtk_init (& argc, & argv );

Window = gtk_window_new (gtk_window_toplevel );
Gtk_window_set_title (gtk_window (window), "gtimer ");
Gtk_window_set_default_size (gtk_window (window), 150, 50 );

Button = gtk_button_new_with_label ("press me ");

Gtk_container_add (gtk_container (window), button );

G_signal_connect (gtk_object (button), "clicked ",
G_callback (button_pressed), "Do something ");

G_signal_connect (gtk_object (window), "Destroy ",
G_callback (gtk_main_quit), null );

G_idle_add (gsourcefunc) idle_callback, "No action ");

Gtk_widget_show_all (window );

Gtk_main ();

Return 0;
}

Similarly, if the specified idle function is set to false, the idle function is removed.

G_idle_add_full ():

Guint g_idle_add_full (Gint priority,



Gsourcefunc function,




Gpointer data,




Gdestroypolicy notify );


The vertex number is similar to g_timeout_add_full.



 



Bloggerads warning
Grasp the final benefits!
Min Chen is a motorfamily
The Japanese market has appreciated. Mitsubishi will not renew this month. A car will send HTC smart phones again, and a new car will be available again. Go!
More messages ..
Degrees of attention, energy, and carbon
A little bright, 2011 yuan, new hopes, saving money and secret technology, and now joining the National shopping Festival
More messages ..
Impr


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.