Sample Code 467: How to set up a stopwatch accurate to hundredth of a second? |
|
Written by kksou
|
|
Wednesday, 02 April 2008 |
|
Problem The method we've used in How to set up a countdown timer - Part 3 - accurate to hundredth of a second? allows us to set up a stopwatch accurate to a hundredth of a second as shown below:

Solution
Sample Code 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
| <?php $window = new GtkWindow(); $window->set_title($argv[0]); $window->set_size_request(400, 200); $window->connect_simple('destroy', array('Gtk','main_quit')); $window->add($vbox = new GtkVBox());
// display title
$title = new GtkLabel("Stopwatch accurate to a hundredth of a second"); $title->modify_font(new PangoFontDescription("Times New Roman Italic 10")); $title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff")); $title->set_size_request(-1, 40); $vbox->pack_start($title, 0); $vbox->pack_start(new GtkLabel(), 0);
$vbox->pack_start(new GtkLabel('Click the button to start.'), 0); $vbox->pack_start(new GtkLabel('Click again to stop.'), 0); $vbox->pack_start(new GtkLabel(), 0, 0);
$vbox->pack_start($hbox = new GtkHBox(), 0, 0); $hbox->pack_start($button = new GtkButton('Start'), 1, 0); $vbox->pack_start(new GtkLabel()); $button->connect('clicked', 'on_button');
$progress = new GtkLabel(); $progress->modify_font(new PangoFontDescription("Times New Roman Italic 20")); $vbox->pack_start($progress);
$window->show_all(); Gtk::main();
function on_button($button) { global $timeout_ID; if ($button->get_label() == 'Start') { global $start_time; $start_time = microtime(1); $timeout_ID = Gtk::timeout_add(1, 'process_task'); // note 1
$button->set_label('Stop'); } else { Gtk::timeout_remove($timeout_ID); $button->set_label('Start'); return false; }
|
- Note that this is only 70% of the sample code. You have to be a registered member to see the entire sample code. Please login or register.
- Registration is free and immediate.
- Have some doubt about the registration? Please read this forum article.
Explanation The above code is based on How to set up a countdown timer - Part 3 - accurate to hundredth of a second?
What's new here:
- Note that I set the timeout event at 1 order magnitude higher, i.e. at 1ms interval. This is because I feel the timeout event may get delayed sometimes if the system is busy or the event queue is long. If the stopwatch were to be accurate to a hundredth of a second, it's better to set the timeout event an order of magnitude higher than the desired accuracy.
- Calculate the elapsed time to the hundredth of a second.
Related Links
User reviews There are no user reviews yet. Note: You have to be a registered member to leave a comment. Free registration here. |
|