Tôi muốn mô phỏng giao tiếp với máy chủ. Khi máy chủ từ xa sẽ có một số chậm trễ Tôi muốn sử dụng một sợi nền có trên đóNSThread block sleepfortimeinterval chủ đề chính
[NSThread sleepForTimeInterval:timeoutTillAnswer];
Các chủ đề được tạo ra với NSThread phụ classing và bắt đầu ... Tuy nhiên tôi nhận thấy rằng sleepForTimeInterval chặn các chủ đề chính. .. Tại sao??? Không phải là một NSThread một backgroundThread theo mặc định?
Đây là cách các chủ đề được tạo:
self.botThread = [[PSBotThread alloc] init];
[self.botThread start];
thông tin bổ sung: Đây là bot chủ đề subclas
- (void)main
{
@autoreleasepool {
self.gManager = [[PSGameManager alloc] init];
self.comManager = [[PSComManager alloc] init];
self.bot = [[PSBotPlayer alloc] initWithName:@"Botus" andXP:[NSNumber numberWithInteger:1500]];
self.gManager.localPlayer = self.bot;
self.gManager.comDelegate = self.comManager;
self.gManager.tillTheEndGame = NO;
self.gManager.localDelegate = self.bot;
self.comManager.gameManDelegate = self.gManager;
self.comManager.isBackgroundThread = YES;
self.comManager.logginEnabled = NO;
self.gManager.logginEnabled = NO;
self.bot.gameDelegate = self.gManager;
BOOL isAlive = YES;
// set up a run loop
NSRunLoop *runloop = [NSRunLoop currentRunLoop];
[runloop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];
[self.gManager beginGameSP];
while (isAlive) { // 'isAlive' is a variable that is used to control the thread existence...
[runloop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
}
}
- (void)messageForBot:(NSData *)msg
{
[self.comManager didReceiveMessage:msg];
}
tôi muốn gọi là "messageForBot" từ các chủ đề chính ... cũng là chủ đề nền nên gọi một phương pháp trên sợi chính để giao tiếp .. Giấc ngủ cho thời gian xen kẽ trong bên trong đối tượng gManager ....
Hiện làm thế nào bạn đang tạo các chủ đề , nó làm gì và khi nào bạn gọi 'sleepForTimeInterval:' (nó trì hoãn luồng hiện tại tại thời gian nó được gọi). – Wain
Đó là cách tôi tạo chuỗi ... [self.botĐầu tiên đã đọc]; ... Điều đó gọi phương thức chính của lớp con NSThread PSBotThread ... – user1028028
Nhưng nó làm gì? 'SleepForTimeInterval' ở đâu? – Wain