2011-04-24 83 views
0

我一直在尋找並試圖爲自己編程,這個問題的答案。目標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 
    } 
} 

回答

1

您不能運行在一個線程的計時器,並在同一時間睡覺的線程。你可能想重新考慮你是否需要一個線程。

有幾件事情需要在這裏指出。首先,當您安排您的計時器:

betLooper = [NSTimer scheduledTimerWithTimeInterval:1 
              target:self 
              selector:@selector(betLoop:) 
              userInfo:nil 
              repeats:YES]; 

它添加到和當前運行的循環by that method保留下來,所以你不需要做手工。只需[myRunLoop run]。你的計時器的選擇參數也是無效的 - 一個計時器的「目標方法」 needs to look like this

- (void)timerFireMethod:(NSTimer *)tim; 

這也意味着你不需要保留的計時器,如果你想要做的是無效的,因爲你將從該方法內部引用它。

其次,目前還不清楚「這個線程需要睡覺等待輸入」的含義。當您安排該計時器時,將在相同的線程上調用方法(betLoop)。如果你要睡覺的話,定時器也會停止。

您似乎對方法/線程有點混淆。 方法betting正在運行您的線程。它本身不是一個線程,並且可以調用betting的其他方法,該方法也將在該線程上。如果你想有一個方法來等待,直到另一個方法完成,您只需撥打第一個內部的第二種方法:

- (void)doSomethingThenWaitForAnotherMethodBeforeDoingOtherStuff { 
    // Do stuff... 
    [self methodWhichINeedToWaitFor]; 
    // Continue... 
} 

我想你只想讓betting回報;運行循環將使線程繼續運行,正如我所說的,從線程上的方法調用的其他方法也在線程中。然後,當你已經做了倒計時,調用另一個方法做需要做的事情(你也可以無效裏面betLoop:定時器)力所能及的工作,並最終確定螺紋:

- (void)takeCareOfBusiness { 
    // Do the things you were going to do in `betting` 
    // Make sure the run loop stops; invalidating the timer doesn't guarantee this 
    CFRunLoopStop(CFRunLoopGetCurrent());  
    return; // Thread ends now because it's not doing anything. 
} 

最後,由於計時器的方法是在同一個線程上,你不需要使用performSelector:onThread:...;只是正常調用方法。

你應該看看Threading Programming Guide

此外,不要忘記釋放您創建的bettingThread對象。

+0

感謝您的答案,新的運行多個線程,感謝您的鏈接,給了它一個閱讀和它相當有幫助。 – Ian 2011-04-24 22:35:32

+0

@Ian:很高興能夠提供幫助,很高興您閱讀文檔! – 2011-04-25 07:32:05

0

NSThread具有一個類方法+ (void)sleepForTimeInterval:(NSTimeInterval)ti。看看這個:)。

+1

這將停止定時器的運行,所以它只能用作定時器的替代品,即如果定時器沒有做其他事情。 – 2011-04-24 20:34:44

+0

這個問題現在是多餘的。 – Ian 2011-04-24 20:37:33

+0

對不起,這個可憐的評論 我的代碼突然開始做所需的行爲,一旦我恢復到早期版本的所有。我不確定這是爲什麼,但我不會在嘴裏看起來像個gifthorse。 澄清: 早些時候我的代碼正在執行計時器,然後當它在某處運行時,繼續通過for循環運行addtional計時器,這不再是這種情況。我不確定爲什麼,但我現在不再需要回答這個問題的最初原因。 感謝您的努力 – Ian 2011-04-24 20:40:28