這裏是我的場景....大中央調度併發
我有一個核心的MIDI應用程序,檢測注意和注意關閉消息,這是很好地工作。
我有一些midiSend方法發送消息回控制器照亮指示燈 - 也工作正常。
我現在想要做的是在注意關閉消息LED指示燈閃爍。這是我的代碼:
[midiListener performSelectorOnMainThread:@selector(startTimer:) withObject:midiMsgParts waitUntilDone:YES];
-(void)startTimer:(NSDictionary *)dict {
ledIntervalCount = 0;
ledIntervalTimer = [NSTimer scheduledTimerWithTimeInterval:0.3
target:self
selector:@selector(ledIntervalLoop:)
userInfo:dict
repeats:YES];
}
-(void)ledIntervalLoop:(NSTimer *)inboundTimer{
NSDictionary *userInfo = [NSDictionary dictionaryWithDictionary:[inboundTimer userInfo]];
NSLog(@"%@", userInfo);
UInt32 onCommand = [[userInfo objectForKey:@"noteOn"] intValue];
//UInt32 offCommand = [[userInfo objectForKey:@"noteOff"] intValue];
UInt32 theNote = [[userInfo objectForKey:@"note"] intValue];
ledIntervalCount++;
if (ledIntervalCount > 3) {
[ledIntervalTimer invalidate];
ledIntervalTimer = nil;
} else {
if(ledIntervalCount %2){
[self sendNoteOnIlluminate:onCommand midiNote:theNote];
}else{
[self sendNoteOnCommand:onCommand midiNote:theNote];
}
}
}
所以我使用NSTimer
替代LED開/關命令。當我按下一個按鈕時它工作正常,但當我同時按下多個按鈕時不會。它看起來只是在最後一次調用startTimer方法時才選擇。
這是我認爲我需要用GCD實現一個調度隊列的地方。這樣每個NSTimer
將全部執行,而不會被隨後的方法調用中斷。
我正確嗎? GCD是否允許我同時運行NSTimer
?
GCD對我來說是一個新概念,所以關於如何實施它的一些指導將有所幫助。我已閱讀了一些參考指南,但需要在我的場景中查看一些示例代碼。我想我在這裏問的是,我的代碼的哪一部分會在塊中?
你可以有N次沒有GCD /許多線程 – 2013-02-10 16:47:46
你說它沒有給你想要的結果,所以我請你說說你想做什麼。 – 2013-02-10 19:34:22
啊你無論如何都會使計時器無效...... 3次嘗試後。 ALL - X定時器需要X個計數器,X定時器有1個計數器 – 2013-02-10 20:57:48