我一直在尋找並試圖爲自己編程,這個問題的答案。目標C:阻塞線程直到NSTimer完成(iOS)
Ive得到了我的MAINVIEW控制器內運行,然後運行的倒計時到0
雖然這種定時器運行的啓動計時器應該暫停輔助線程計時器的輔助線程/阻塞不管。
當計時器達到0時,輔助線程應該繼續。
Ive用NSCondition和NSConditionLock都無濟於事,所以id理想情況下像解決我的代碼問題的解決方案,或者指向我如何解決這個問題的指南。不是那些簡單陳述「使用X」的人。
- (void)bettingInit {
bettingThread = [[NSThread alloc] initWithTarget:self selector:@selector(betting) object:nil];
[bettingThread start];
}
- (void)betting {
NSLog(@"betting Started");
for (int x = 0; x < [dealerNormalise count]; x++){
NSNumber *currSeat = [dealerNormalise objectAtIndex:x];
int currSeatint = [currSeat intValue];
NSString *currPlayerAction = [self getSeatInfo:currSeatint objectName:@"PlayerAction"];
if (currPlayerAction != @"FOLD"){
if (currPlayerAction == @"NULL"){
[inactivitySeconds removeAllObjects];
NSNumber *inactivitySecondsNumber = [NSNumber numberWithInt:10];
runLoop = [NSRunLoop currentRunLoop];
betLooper = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(betLoop) userInfo:nil repeats:YES];
[runLoop addTimer:[betLooper retain] forMode:NSDefaultRunLoopMode];
[runLoop run];
// This Thread needs to pause here, and wait for some input from the other thread, then continue on through the for loop
NSLog(@"Test");
}
}
}
}
- (void)threadKiller {
[betLooper invalidate];
//The input telling the thread to continue can alternatively come from here
return;
}
- (void)betLoop {
NSLog(@"BetLoop Started");
NSNumber *currentSeconds = [inactivitySeconds objectAtIndex:0];
int currentSecondsint = [currentSeconds intValue];
int newSecondsint = currentSecondsint - 1;
NSNumber *newSeconds = [NSNumber numberWithInt:newSecondsint];
[inactivitySeconds replaceObjectAtIndex:0 withObject:newSeconds];
inacTimer.text = [NSString stringWithFormat:@"Time: %d",newSecondsint];
if (newSecondsint == 0){
[self performSelector:@selector(threadKiller) onThread:bettingThread withObject:nil waitUntilDone:NO];
// The input going to the thread to continue should ideally come from here, or within the threadKiller void above
}
}
感謝您的答案,新的運行多個線程,感謝您的鏈接,給了它一個閱讀和它相當有幫助。 – Ian 2011-04-24 22:35:32
@Ian:很高興能夠提供幫助,很高興您閱讀文檔! – 2011-04-25 07:32:05