Sleep or Pause NSThread

寵の児 提交于 2019-12-10 15:30:58

问题


I am creating a new Thread which runs one of my method after every certain period of time.

Now what i am doing is as follows:

NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(setUpTimerThread) object:nil];
 [thread start];

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.

i have used

[NSThread sleepForTimeInterval:3.0];

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?


回答1:


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.

replace your current sleep call with

[self performSelector:@selector(sleepThread) onThread:thread withObject:nil waitUntilDone:YES];

then implement this method

-(void)sleepThread {
    [NSThread sleepForTimeInterval:3.0];
}



回答2:


The solution of ColdLogic is right, but it's neccesary put the flag waitUntilDone to YES, otherwise the method is never called.



来源:https://stackoverflow.com/questions/7952653/sleep-or-pause-nsthread

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!