0

一定時間後にタイムアウトしたい NSThread があります。

[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
}

これについてあなたが提供できる助けは大歓迎です。

ありがとう、

Zen_silence

4

2 に答える 2

2

NSThread を使用する代わりに、NSOperation を使用します。その後、操作への参照を保持し、NSTimer を 10 秒間設定できます。タイマーが起動したら、操作自体をキャンセルするように指示します。

于 2010-03-18T21:47:43.627 に答える
1

次のようなことを試してください:

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];

(1)workerThread.isCancelledスレッドが終了したときにスレッドがタイムアウトしたかどうかを確認し、(2) スレッドがタイムアウトし[timoutTimer invalidate]なかった場合はタイマーをクリーンアップするために呼び出します。

于 2010-03-18T21:54:34.067 に答える