Snippet. Vala. Stop the Execution of Program for Specified Time

This Vala snippet shows how to stop the execution of a program for a specified amount of time.

Using the class Timer from the Glib library the program can easily be stopped. The method elapsed returns the elapsed time since the timer has been started. The returned value from the method is double.

var timer = new Timer();

timer.start();

while(timer.elapsed() <= 5.0); // Stops program for 5 seconds

timer.stop();

The same can be achieved with a for loop.

var timer = new Timer();

timer.start();

// Stops program for 5 seconds
for (double x = 0.0; x <= 5.0;) { x = timer.elapsed(); }  

timer.stop();

Updated on: 24 Apr 2024