[英]high resolution timer freezing the rest of my app
我们有一个很棒的应用程序(涉及cocos2d)。 在我的课堂上,我必须实现大约1毫秒或更短的高分辨率计时器。
计时器类正在工作,但是因为我有一个while(1), 所以应用程序的其余部分甚至都没有加载,并且屏幕是黑色的-它仅处理我的while(1)
这是我的代码:
-(void)HighResolutionTimer
{
while(1)
{
if(TimerFlag==0) //take time once
{
start = [NSDate date];
TimerFlag=1;
}
end= [NSDate date]; //check timer
if([end timeIntervalSinceDate:start]*1000.0f >0.5) //wait for 1ms
{
TimerFlag=0;
[self sampleAudio]; //do my job(with NSLog)
}
if(stopTimer==1) //break
{stopTimer=0; break;}
}
}
我可以向while(1)输入什么以便让处理器并行执行其他操作 ? 这种方法对iPhone处理器来说太重了吗? 还有另一种方法来使每个1ms呼叫某个对象吗?
我可以向while(1)输入什么以便让处理器并行执行其他操作?
您可以使用GCD将这段代码放入另一个线程中
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^ {
[self HighResolutionTimer];
});
这种方法对iPhone处理器来说太重了吗?
当然,它基本上是无限循环。
还有另一种方法来使每个1ms呼叫某个对象吗?
您是否尝试过使用NSTimer
? 尝试
NSTimer *timer = NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.1f
target:self
selector:@selector(HighResolutionTimer)
userInfo:nil
repeats:YES];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.