睡眠功能的替代方案

发布于 2024-08-05 09:16:33 字数 374 浏览 1 评论 0原文

我在我的应用程序中实现了以下内容。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

余罪 2024-08-12 09:16:33

还有 NSThread 的 +sleepForTimeInterval:,它需要以秒为单位的时间。例如:

[NSThread sleepForTimeInterval:0.05];

应该休眠 50 毫秒。

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

[NSThread sleepForTimeInterval:0.05];

should sleep for 50 milliseconds.

橙味迷妹 2024-08-12 09:16:33

正如您启动的另一个线程中所提到的,您可能不想在主线程上休眠,因为这会阻止事件传递并冻结 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.

云巢 2024-08-12 09:16:33

您可能需要 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文