Gtimer is a timer, which can be used whenever you need to specify two periods of time. For example, the start time and end time of the program line, you can use g_timer_new () to create a new gtimer. If you no longer need it, use g_timer_destroy () to renew it.
After g_timer_new (), the automatic scaling time is displayed. You can also use g_timer_start () to re-mark the scaling time, and at g_timer_elapsed () the time after the callback is triggered.
If
Use g_timer_start () to indicate the time when the cursor is triggered, and use g_timer_end () to indicate the end time.
When g_timer_elapsed () is called, it will return to the gap between the time after the operation and the end time. After g_timer_end () is used to indicate the end time, you can use g_timer_continue () to re-compile gtimer.
The following program is a simple example. You can calculate the time interval between two instances:
#include <gtk/gtk.h>
void button_pressed(GtkButton *button, GTimer *timer) {
static gdouble elapsedTime;
static gboolean isRunning = FALSE;
if(isRunning) {
elapsedTime = g_timer_elapsed(timer, NULL);
gtk_button_set_label(button, "Start");
g_print("Elapsed Time: %f s/n", elapsedTime);
}
else {
g_timer_start(timer);
gtk_button_set_label(button, "Stop");
}
isRunning = !isRunning;
}
int main(int argc, char *argv[]) {
GtkWidget *window;
GtkWidget *button;
GTimer *timer;
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("Start");
gtk_container_add(GTK_CONTAINER(window), button);
timer = g_timer_new();
g_signal_connect(GTK_OBJECT(button), "clicked",
G_CALLBACK(button_pressed), timer);
g_signal_connect(GTK_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
The result of a line is as follows: