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

发布于 2024-09-08 08:04:04 字数 884 浏览 4 评论 0原文

这是代码:

- (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 设置的预设音量。

有什么想法吗?

以利亚

Here is the code:

- (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, @"", @"", @"");
    }
}

So when you press the button "startRecognition" then it starts this timer loop "timer fired". BUT when the value charlieSoundVolume is less than backgroundSoundLevel, it freezes the app. When it's greater, it works fine. So there's something wrong with that part of the code. I'm not really sure what...

Background info: charlieSoundVolume is the current volume expressed in an NSString. backgroundSoundVolume is also expressed in an NSString. The charlieSoundVolume is the current volume and the backgroundSoundVolume is the preset volume set by the NSSlider backGroundSoundChange.

Any ideas??

Elijah

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

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

发布评论

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

评论(1

扛起拖把扫天下 2024-09-15 08:04:04

这是工作代码:

- (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, @"", @"", @"");
    }
}

Here is the working code:

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