GTK gossip: gtimer

Source: Internet
Author: User
Tags gtk

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:

  • G_timer_demo.c
#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:



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.