views:

1319

answers:

4

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 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?

thanks in advance for sharing your knowledge.

sagar

+2  A: 

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.

AlBlue
nanosleep(<#const struct timespec * #>, <#struct timespec * #>)Sir, Would you please tell me, what to pass as arguments?
sugar
+1  A: 

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

crashmstr
excellent one. Great knowledge.
sugar
finally I implemented this. !stopThroughButtons i++){ if(i>=9) break; [NSThread detachNewThreadSelector:@selector(moveNeedle) toTarget:self withObject:nil]; usleep(200000); }
sugar
See cdespinosa's answer below. Using sleep() is almost always the wrong answer.
bbum
sugar
+2  A: 

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

[NSThread sleepForTimeInterval:0.05];

should sleep for 50 milliseconds.

Brad Larson
+6  A: 

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.

cdespinosa
+1, sleeping is almost always the wrong thing to do
Adam Rosenfield
sugar