线程NSTimer [英] Threaded NSTimer

查看:138
本文介绍了线程NSTimer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有关这个主题的许多问题,因为我之前已经问过一个问题,现在我的问题似乎更多与线程部分相关。我有以下2种方法。

I am aware of the many questions regarding this topic, as I myself have asked one previously however, my issue now seems to be more related to the threading part. I have the following 2 methods.

-(void) restartTimer {

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1. 
                                             target:self
                                           selector:@selector(dim) 
                                           userInfo:nil 
                                            repeats:YES];
    time = 31;
    NSLog(@"calling restart timer");
    [self performSelectorOnMainThread:@selector(timerImageUpdate) withObject:nil waitUntilDone:NO];

    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
    [[NSRunLoop currentRunLoop] run];

    [pool drain];

}

-(void) resumeTimer {

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1. 
                                             target:self
                                           selector:@selector(dim) 
                                           userInfo:nil 
                                            repeats:YES];
    NSLog(@"calling resume timer");

    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
    [[NSRunLoop currentRunLoop] run];

    [pool drain];

}

restartTimer函数在游戏开始时调用。这工作正常,定时器很好地启动昏暗的选择器。当用户快速连续点击我的跳过按钮时,会出现问题。 [skip]更新MKMapView,并在委托方法mapdidfinishloading中调用以下命令:
[NSThread detachNewThreadSelector:@selector(restartTimer)toTarget:self withObject:nil];

当发生这种情况创建了几个计时器,因此我的调光功能被调用太频繁,使得单个计时器运行真的很快的外观?使用辅助线程启动和重新启动计时器的最好方法是什么?注意这个问题似乎只有当跳过按钮反复和快速,而它工作正常,如果只是再按一次按钮?

The restartTimer function is called when the game begins. This works fine and the timer fires the dim selector nicely. The problem occurs when the user clicks my skip button in quick succession. [skip] updates an MKMapView and in the delegate method mapdidfinishloading the follwing is called : [NSThread detachNewThreadSelector:@selector(restartTimer) toTarget:self withObject:nil];
When this happens it seems several timers are created and thus my dim function is called far too often giving the appearance of the single timer running really fast? What is the best way to start and restart the timer using a secondary thread? Note this problem only seems to happen if the skip button is pressed repeatedly and quickly, while it works fine if just pressed now and again?

任何人有任何想法?非常感谢

Anyone have any ideas? Many thanks

Jules

推荐答案

如果你有一个定时器运行或不。当您启动计时器时将其设置为true,当您停止计时器时,将其设置为false。当调用恢复计时器功能时,检查此变量,如果是,则不启动新计时器。

You can use a bool to know if you have a timer running or not. When you start the timer you set it to true, when you stop the timer you set it to false. When resume timer function is called you check this variable and if it's true you do not start a new timer.

另一个解决方案是限制用户与按钮的交互。如果按下按钮,则使其无效一段时间。

Another solution would be to limit user interaction with the button. If the button is pressed you make it inactive for a time.

这篇关于线程NSTimer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆