睡眠或暂停NSThread [英] Sleep or Pause NSThread
问题描述
我正在创建一个新的Thread,它会在每一段时间后运行我的一个方法。
I am creating a new Thread which runs one of my method after every certain period of time.
现在我正在做的如下:
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(setUpTimerThread) object:nil];
[thread start];
上面的方法调用我的setUpTimerThraed方法,这个方法生成一个NSTimer并调用一个特定的方法,即syncData每1分钟后。现在我想要的是我的同步数据方法是将一些文件发送到FTP文件夹。什么时候它发送一个文件到FTP我想让我的线程睡一会儿。
the above method calls my setUpTimerThraed method, and this method makes an NSTimer and calls a specific method i.e syncData after every 1 minute. Now what i want is my sync Data method is sending some files to the FTP folder. and when ever it send one file to the FTP I want it my "thread" to be sleep for a while.
我用过
[NSThread sleepForTimeInterval:3.0];
但此代码会阻止我的应用程序主线程。有什么方法我只能睡一段时间我的线程线程,以便它不会停止我的应用程序响应3秒,并没有睡觉我的主线程。
希望你得到我要问的人。请帮忙?
But this code blocks my application main thread. Is there any way i can only sleep my "thread" thread for a while so that it wont stop my application responding for 3 seconds and does not sleep my main thread. Hope u get what i am asking guys. PLease help?
推荐答案
sleepForTimeInterval:
睡眠调用它的线程上。尝试在你要睡觉的线程而不是主线程上调用睡眠。
sleepForTimeInterval:
sleeps the thread that it is called on. Try calling a sleep on the thread you want to sleep rather than the main thread.
用
[self performSelector:@selector(sleepThread) onThread:thread withObject:nil waitUntilDone:YES];
然后实施此方法
-(void)sleepThread {
[NSThread sleepForTimeInterval:3.0];
}
这篇关于睡眠或暂停NSThread的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!