please anyone tell me how to use sleep() for few milliseconds in swift 2.2?
while (true){ print("sleep for 0.002 seconds.") sleep(0.002) // not working }
but
while (true){ print("sleep for 2 seconds.") sleep(2) // working }
it is working.
You need to call Task. sleep() using await as it will cause the task to be suspended, and you also need to use try because sleep() will throw an error if the task is cancelled. Important: Calling Task. sleep() will make the current task sleep for at least the amount of time you ask, not exactly the time you ask.
usleep() takes millionths of a second
usleep(1000000) //will sleep for 1 second usleep(2000) //will sleep for .002 seconds
OR
let ms = 1000 usleep(useconds_t(2 * ms)) //will sleep for 2 milliseconds (.002 seconds)
OR
let second: Double = 1000000 usleep(useconds_t(0.002 * second)) //will sleep for 2 milliseconds (.002 seconds)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With