Objective-C类时间延迟?

发布于 2024-07-25 15:03:04 字数 136 浏览 4 评论 0原文

我想知道是否有人知道 Objective-C 中的一个类可以在运行时的指定时间停止/延迟应用程序? 我已经查看了时间管理器和 NSTimer,但不知道如何在其中添加延迟。 我以前从未使用过这两个类,所以也许其他人已经使用过并且可以给我一些提示? 谢谢!

I was wondering if anyone knows of a class in objective-C that stops/delays the application at a specified amount of time during runtime? I've already looked at the Time Manager and NSTimer and don't see how I could possibly put a delay in there. I've never used these two classes before though, so maybe someone else has and could give me a tip? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

演多会厌 2024-08-01 15:03:04

这实际上取决于您所说的“停止/延迟应用程序”的含义。 如果您有一个单线程应用程序,您可以使用各种睡眠调用中的任何一个(usleep、sleep、nanosleep 等)。 但这可能不是您想要的,因为它只会导致用户的 UI 挂起,并且不会停止任何后台线程。

要真正给出一个像样的答案,可能有必要知道您实际上想要做什么。 您是否在等待某些事情,轮询资源等?

It really depends what you mean by "stops/delays the app." If you have a single threaded app you can just use anyone of the various sleep calls (usleep, sleep, nanosleep, etc). That is probably not what you want though, because it will just result in a hung UI for the user, and won't stop any background threads.

To actually give a decent answer it is probably necessary to know what you are actually trying to do. Are you waiting for something, polling a resource, etc?

青丝拂面 2024-08-01 15:03:04
#include <unistd.h>
sleep(3); // or usleep(3000000);
#include <unistd.h>
sleep(3); // or usleep(3000000);
三五鸿雁 2024-08-01 15:03:04

我认为您正在寻找 [NSThread sleepForTimeInterval:] 这应该可以解决问题。 您还应该考虑使用计时器在延迟后调用另一个函数,因为该解决方案是非阻塞的。 如果您从主线程调用 NSThread 方法,它将在时间间隔内完全锁定您的应用程序。

I think you're looking for [NSThread sleepForTimeInterval:] That should do the trick. You should also consider using a timer to invoke another function after a delay, because that solution is non-blocking. The NSThread approach will totally lock your application for the time interval if you call it from the main thread.

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