iPhone xcode 中的 nsthread

发布于 2024-08-31 02:50:27 字数 813 浏览 3 评论 0原文

再会!

我想在 xcode 项目中使用 nsthreads,该项目将调用一个用于网络访问的函数,并检查网络是否存在,所以我需要一个线程,该线程将在 1 分钟后执行以进行检查连接性。并将继续运行,除非应用程序关闭。

[NSThread detachNewThreadSelector:@selector(startTheBackgroundJob) toTarget:self withObject:nil];

startTheBackgroundJob

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// wait for 3 seconds before starting the thread, you don't have to do that. This is just an example how to stop the NSThread for some time
[NSThread sleepForTimeInterval:5];
//[self performSelectorInBackground:@selector(checkNet) withObject:nil];
[self performSelectorOnMainThread:@selector(checkNet) withObject:nil waitUntilDone:YES];
[pool release];

它仅在第一次工作,但在其他任何时候都不起作用,我的意思是它只进行了 1 个循环,

有人可以在这方面帮助我。

谢谢

Good Day!

i want to use nsthreads in a project of xcode that will call a function which is for the network access and will check that if the network is there or not, so i need to have a thread which will execute after lets say 1 minutes to check the connectivity. and will continue run unless the app is closed.

[NSThread detachNewThreadSelector:@selector(startTheBackgroundJob) toTarget:self withObject:nil];

startTheBackgroundJob

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// wait for 3 seconds before starting the thread, you don't have to do that. This is just an example how to stop the NSThread for some time
[NSThread sleepForTimeInterval:5];
//[self performSelectorInBackground:@selector(checkNet) withObject:nil];
[self performSelectorOnMainThread:@selector(checkNet) withObject:nil waitUntilDone:YES];
[pool release];

it works only for the first time but not any other, i mean only 1 loop it makes

can somebody help me in this regard.

Thanks

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

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

发布评论

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

评论(3

傲娇萝莉攻 2024-09-07 02:50:27

您可以使用 NStimer 代替...将 Property Repeated 设置为 YES

You can use NStimer instead... With Property Repeated set to YES

不气馁 2024-09-07 02:50:27

当您需要执行冗长的任务,但不希望它阻止应用程序其余部分的执行时,线程特别有用。特别是,您可以使用线程来避免阻塞应用程序的主线程。

为了了解更多....
从中找到简单而简短的源代码
http://www.xprogress.com/post-36-threading-tutorial-using-nsthread-in-iphone-sdk-objective-c/" xprogress.com/post-36-threading-tutorial-using-nsthread-in-iphone-sdk-objective-c/

Threads are especially useful when you need to perform a lengthy task, but don’t want it to block the execution of the rest of the application. In particular, you can use threads to avoid blocking the main thread of the application.

To understand more....
find nice simple and short source code from
http://www.xprogress.com/post-36-threading-tutorial-using-nsthread-in-iphone-sdk-objective-c/

我建议结合 NSTimer(每 5-10 秒触发一次)+ Reachability 接口(检查网络状态)。不需要线程。要使用 Reachability 检查 Apple 的示例。

将此调用放入 viewDidLoad 中

[NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(onTimer:) userInfo:nil repeats:YES];

-(void) onTimer:(NSTimer *)theTimer 
{
    //Here perform reachability checks
}

I'd suggest a combination of NSTimer, that fires each 5-10 seconds + a Reachability interface, which checks for you the network status. No need for a thread. For using the Reachability check Apple's example.

Put this call in viewDidLoad

[NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(onTimer:) userInfo:nil repeats:YES];

-(void) onTimer:(NSTimer *)theTimer 
{
    //Here perform reachability checks
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文