Re: [Vala] problem using sleep
- From: Frederik <scumm_fredo gmx net>
- To: vala-list <vala-list gnome org>
- Subject: Re: [Vala] problem using sleep
- Date: Sun, 11 Oct 2009 10:46:29 +0200
a a wrote:
Thanks it's working.
I'd like to display the remaining time[maybe inside a label?].
How can I do it?
Thanks a lot.
This class does what you need:
class Countdown {
private int time_min;
private uint timeout_tag;
private TimeVal start_time;
public bool active { get; private set; }
public signal void update ();
public signal void liftoff ();
public Countdown (int time_min) {
this.time_min = time_min;
}
public void start () {
this.start_time = TimeVal ();
this.active = true;
this.timeout_tag = Timeout.add (1000, () => {
if (get_remaining_seconds () > 0) {
update ();
return true;
} else {
stop ();
liftoff ();
return false;
}
});
update ();
}
public void stop () {
Source.remove (this.timeout_tag);
this.active = false;
update ();
}
public long get_remaining_seconds () {
var current = TimeVal ();
var elapsed = current.tv_sec - this.start_time.tv_sec;
return this.time_min * 60 - elapsed;
}
public string get_time_formatted () {
var remaining = get_remaining_seconds ();
return "%ld:%02ld".printf (remaining / 60, remaining % 60);
}
}
Usage:
var countdown = new Countdown (5);
countdown.update.connect (() => {
if (countdown.active) {
label.set_text ("Time remaining: " + countdown.get_time_formatted ());
} else {
label.set_text ("Countdown not started");
}
});
countdown.liftoff.connect (() => {
// spawn process
});
start_button.clicked.connect (() => countdown.start ());
stop_button.clicked.connect (() => countdown.stop ());
Best regards,
Frederik
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]