Re: [Vala] repeat every n seconds



On 21/10/11 01:30, "Luis L. Rodríguez Oro" wrote:
Hello friends,

I need run a function indefinitely every N secods, something like this.

using GLib;
int seconds = 5;

bool myfunction(){
     stdout.printf ("ok\n");

     return true;
}


void main () {
     //how run my function every 5 seconds
     myfunction(); // this run only one time
}

Use GLib.Timeout.add(), it will run a method every n milliseconds until
that method returns false. For this to work, you must also run a
MainLoop to keep the program alive (however you may already have a main
loop if this is part of a Gtk application or something)

For example:

int seconds = 5;

bool myfunction(){
    stdout.printf ("ok\n");
    return true;
}


void main () {
    Timeout.add(seconds * 1000, myfunction);
    var m = new MainLoop();
    m.run();
}


(BTW you don't need to have using GLib; at the start of the file, Vala
automatically assumes this is there)

Hope this helps :)

--
Andrew



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]