I wrote a program to test dart’s timers:
#!/home/jim/dart/dart-sdk/bin/dart –enable-checked-mode
#import(“dart:isolate”);void
zero(Timer timer) {
print(“zero\n”);
}class Timee {
Timer timer;
int id;Timee(id) {
this.id = id;
this.timer = new Timer(id, timeout);
}void
timeout(Timer timer) {
print(“timeout $id\n”);
}
}void
main() {
var zero = new Timer(0, zero);
print(“created zero\n”);
var one = new Timee(1);
var two = new Timee(2);
print(“end of main\n”);
}
This produces the following output:
created zero
end of main
zero
timeout 1
timeout 2
The first interesting thing is that the timers go off after the main program has exited. This is because, as long as there is unfinished work, dart will continue to process events. The second interesting thing is that timer callbacks are functors. They include the function or method and, in the case of a method, the object (instance) to call it on.
The last and most interesting thing is that the timer with duration 0 is still deferred until after the main program exits. This means that using a zero length timer lets you avoid calling back during a forward call, which can lead to really tricky reentrancy problems for the callers of asynchronous libraries.