睡眠功能的替代方案
我在我的应用程序中实现了以下内容。
for(i=0;!stopThroughButtons && i<totalMovements;i++){
[NSThread detachNewThreadSelector:@selector(moveNeedle) toTarget:self withObject:nil];
sleep(0.3);
}
这里的 sleep
是一个参数为 unsigned int
类型的函数。
睡眠方法使用秒数来睡眠。
我想给出以毫秒为单位的睡眠时间。
有哪些最佳替代方案可用?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
还有 NSThread 的 +sleepForTimeInterval:,它需要以秒为单位的时间。例如:
应该休眠 50 毫秒。
There's also NSThread's +sleepForTimeInterval:, which takes time in fractional seconds. For example:
should sleep for 50 milliseconds.
两种选择:
int usleep(useconds_t useconds)
将使用微秒int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
将使用纳秒Two options:
int usleep(useconds_t useconds)
will use microsecondsint nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
will use nanoseconds正如您启动的另一个线程中所提到的,您可能不想在主线程上休眠,因为这会阻止事件传递并冻结 UI。您可能想要触发计时器并在计时器触发时执行您的操作,而不是循环和睡眠。
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.
您可能需要
nanosleep()
来代替,这需要纳秒。请记住,这些睡眠时间并不准确,因此您真正拥有的是比百分之几秒更准确地指定它的能力。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.