线程 NSTimer

发布于 2024-09-07 23:45:25 字数 1738 浏览 1 评论 0原文

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

-(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];
当发生这种情况时,似乎会创建多个计时器,因此我的昏暗函数被调用得太频繁,从而使单个计时器看起来运行得非常快?使用辅助线程启动和重新启动计时器的最佳方法是什么?请注意,此问题似乎仅在重复快速按下“跳过”按钮时才会发生,而如果只是偶尔按下则可以正常工作?

有人有什么想法吗?非常感谢

朱尔斯

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];

}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝色星空 2024-09-14 23:45:26

您可以使用布尔值来了解计时器是否正在运行。当您启动计时器时,您将其设置为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文