sagarkothari
sagarkothari

Reputation: 24820

alternative for sleep function

I have implemented following in my application.

for(i=0;!stopThroughButtons && i<totalMovements;i++){
        [NSThread detachNewThreadSelector:@selector(moveNeedle) toTarget:self withObject:nil];
        sleep(0.3);
    }

Here the sleep is a function with argument of unsigned int type.

Sleep method uses seconds to sleep.

I want to give the timing in milliseconds to sleep.

Which best alternate is available?

Upvotes: 9

Views: 34065

Answers (4)

cdespinosa
cdespinosa

Reputation: 20799

As has been mentioned in the other thread you started, you probably don't want to sleep on the main thread, as that blocks event delivery and freezes the UI. Instead of looping and sleeping, you probably want to fire a timer and perform your action when the timer fires.

Upvotes: 16

Brad Larson
Brad Larson

Reputation: 170317

There's also NSThread's +sleepForTimeInterval:, which takes time in fractional seconds. For example:

[NSThread sleepForTimeInterval:0.05];

should sleep for 50 milliseconds.

Upvotes: 37

crashmstr
crashmstr

Reputation: 28583

Two options:
int usleep(useconds_t useconds) will use microseconds
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp) will use nanoseconds

Upvotes: 34

AlBlue
AlBlue

Reputation: 24060

You probably want nanosleep() instead, which takes nanoseconds. Bear in mind these sleep times are not accurate though, so what you really have is the ability to specify it more accurately than a few hundredths of seconds.

Upvotes: 3

Related Questions