我想用NSTimer
以每页20秒跑完一个功能,但我把NSTimer
在NSThread
function.the代码是这样的:NSTimer无法在NSThread函数中启动或运行?
-(void)func1
{
[NSThread detachNewThreadSelector:@selector(func2)
toTarget:self withObject:nil];
}
-(void)func2
{
[NSTimer scheduledTimerWithTimeInterval:3 target:self
selector:@selector(func3) userInfo:nil repeats:YES];
}
-(void)func3
{
//do something,but the function seems never be executed
}
所以问题是,FUNC3从未被执行NSTimer
in func2.有人说NSTimer
只能运行在main thread
。这是真的吗??所以不能在NSTimer
启动一个NSThread
函数?
@Thomasso你的意思是NSTimer可以在NSThread中设置和运行,不仅在主线程中? – SgViTiNer 2013-03-12 02:57:44
如果您有长时间运行的线程,那么您可以在其上设置一个定时器。长时间运行的线程就像是一个网络线程,它正在后台下载数据或从相机或麦克风输入线程处理。 – 2013-03-12 03:09:47
它甚至没有去睡觉;线程体是'func2'方法,它调度定时器,然后立即返回,线程退出。 – 2013-03-12 07:40:45