计时器导致应用程序冻结在某些情况下

时间:2010-07-05 02:06:20

标签: objective-c cocoa nstimer

以下是代码:

- (IBAction) startRecognition:(id)sender {
    backgroundSoundLevel = [backgroundSoundChange stringValue];
    timer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
}

- (void)timerFired:(NSTimer*)theTimer
{   
    NSString *charlieSoundVolume = [charlieSoundLevel stringValue];
    if ([charlieSoundVolume isLessThan: backgroundSoundLevel]) {
        NSRunAlertPanel(@"", charlieSoundVolume, @"", @"", @"");
    }
}

因此当您按下“startRecognition”按钮时,它会启动此计时器循环“计时器触发”。但是当charlieSoundVolume值小于backgroundSoundLevel时,它会冻结应用程序。当它更大时,它工作正常。所以这部分代码有问题。我不确定是什么......

背景信息:charlieSoundVolume是NSString中表示的当前卷。 backgroundSoundVolume也以NSString表示。 charlieSoundVolume是当前音量,backgroundSoundVolume是NSSlider backGroundSoundChange设置的预设音量。

任何想法??

利亚

1 个答案:

答案 0 :(得分:0)

以下是工作代码:

- (IBAction) startRecognition:(id)sender {
    timer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
}

- (void)timerFired:(NSTimer*)theTimer
{   
    backgroundSoundLevel = [backgroundSoundChange stringValue];
    NSString *charlieSoundVolume = [charlieSoundLevel stringValue];
    if ([charlieSoundVolume  isLessThan: backgroundSoundLevel]) {
        NSRunAlertPanel(@"", charlieSoundVolume, @"", @"", @"");
    }
}