[src]

Struct std::io::timer::Timer

pub struct Timer {
    // some fields omitted
}

A synchronous timer object

Values of this type can be used to put the current task to sleep for a period of time. Handles to this timer can also be created in the form of receivers which will receive notifications over time.

Example

use std::io::Timer;

let mut timer = Timer::new().unwrap();
timer.sleep(10); // block the task for awhile

let timeout = timer.oneshot(10);
// do some work
timeout.recv(); // wait for the timeout to expire

let periodic = timer.periodic(10);
loop {
    periodic.recv();
    // this loop is only executed once every 10ms
}

If only sleeping is necessary, then a convenience api is provided through the io::timer module.

use std::io::timer;

// Put this task to sleep for 5 seconds
timer::sleep(5000);

Methods

impl Timer

fn new() -> IoResult<Timer>

Creates a new timer which can be used to put the current task to sleep for a number of milliseconds, or to possibly create channels which will get notified after an amount of time has passed.

fn sleep(&mut self, msecs: u64)

Blocks the current task for msecs milliseconds.

Note that this function will cause any other receivers for this timer to be invalidated (the other end will be closed).

fn oneshot(&mut self, msecs: u64) -> Receiver<()>

Creates a oneshot receiver which will have a notification sent when msecs milliseconds has elapsed. This does not block the current task, but instead returns immediately.

Note that this invalidates any previous receiver which has been created by this timer, and that the returned receiver will be invalidated once the timer is destroyed (when it falls out of scope).

fn periodic(&mut self, msecs: u64) -> Receiver<()>

Creates a receiver which will have a continuous stream of notifications being sent every msecs milliseconds. This does not block the current task, but instead returns immediately. The first notification will not be received immediately, but rather after msec milliseconds have passed.

Note that this invalidates any previous receiver which has been created by this timer, and that the returned receiver will be invalidated once the timer is destroyed (when it falls out of scope).