1
我試圖在下面的代碼中使用睡眠,當NSStream連接失敗或者如果有流錯誤並嘗試在睡眠後重新連接。睡眠正在工作,但它使整個應用程序進入睡眠狀態。NSThread的睡眠
我已經將NSStream作爲線程啓動,但是當接收到NSStreamEvent時,handleEvent似乎以同步方式工作。
對於這段代碼使用睡眠的任何想法..?我只想讓睡眠單獨爲Stream線程工作。
- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)event
{
case NSStreamEventErrorOccurred:
{
NSError *streamErr = [stream streamError];
NSString *strErr = [streamErr localizedFailureReason];
[self CloseStream];
NSLog(@"Stream Error ::: %@",strErr);
//[NSThread sleepForTimeInterval : 15];
sleep(15);
[self Initialize];
[self OpenStream];
break;
}
case NSStreamEventEndEncountered:
{
NSLog(@"Connection Closed by the Server");
[self CloseStream];
usleep(15000);
[self Initialize];
[self OpenStream];
break;
}
}
不要忘記dispatch_release :) – jjxtra 2013-01-26 06:06:05