代码之家  ›  专栏  ›  技术社区  ›  Zen_silence

超时一段时间后的NShread

  •  0
  • Zen_silence  · 技术社区  · 16 年前

    [NSThread detachNewThreadSelector:@selector(someFuntion) toTarget:self withObject:nil];
    
    - (void) someFunction {
       //Some calculation that might take a long time.
       //if it takes more then 10 seconds i want it to end and display a error message
    }
    

    如果您能在这方面提供任何帮助,我们将不胜感激。

    谢谢

    禅宗静默

    2 回复  |  直到 16 年前
        1
  •  1
  •   Tom    16 年前

    尝试以下操作:

    workerThread = [[NSThread alloc] initWithTarget:self selector:@selector(someFunction) object:nil];
    [workerThread start];
    timeoutTimer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:workerThread selector:@selector(cancel) userInfo:nil repeats:NO];
    

    一定要检查一下 workerThread.isCancelled 当线程结束时,看线程是否超时,(2)调用 [timoutTimer invalidate] 如果线程未超时,则清除计时器。

        2
  •  2
  •   Steven Canfield    16 年前