Objective-c中线程NSThread的使用
2013-09-27 16:36
363 查看
NSThread使用
1.创建并启动线程
2.停止线程
先向线程发送cancel消息,将线程标记为停止,然后在合适的地方判断线程是否标记为退出,如果是,则发送exit消息,真正退出线程
1.创建并启动线程
thread = [[NSThread alloc]initWithTarget:self selector:@selector(startMonitor) object:nil]; [thread start];
2.停止线程
先向线程发送cancel消息,将线程标记为停止,然后在合适的地方判断线程是否标记为退出,如果是,则发送exit消息,真正退出线程
- (void)getInfo:(id)sender
{
if (!running) {
NSLog(@"start monitor");
thread = [[NSThread alloc]initWithTarget:self selector:@selector(startMonitor) object:nil]; [thread start];
running = true;
}
else{
NSLog(@"stop monitor");
[thread cancel];
running = false;
}
}
- (void) startMonitor
{
while (true) {
if ([[NSThread currentThread] isCancelled]) {
[NSThread exit];
}
//do your things here
sleep(1);
};
}
相关文章推荐
- ADS使用错误 could not open file debugrel \objectcode
- check_traffic.sh报错-No Such Object available on this agent at this OID
- QMetaObject::connectSlotsByName: No matching signal for……
- SQL Server OBJECT_ID() 函数
- windows Objective-C模拟环境搭建
- ATL7中OBJECT_ENTRY_AUTO宏是如何实现com对象映射表
- 【转】Objective-C BOOl、bool比较
- 十分钟让你明白Objective-C的语法(和Java、C++的对比)
- Objective-C语法之代码块(block)的使用
- Objective-C语法之Category的使用
- Practical ATL: Understanding the class object
- Objective-C语法之NSSet和NSMutableSet
- Objective-C语法之动态类型(isKindOfClass, isMemberOfClass,id)等
- Objective-C语法之NSDictionary和NSMutableDictionary
- Objective-C语法之NSArray和NSMutableArray
- Objective-C语法property详解
- Objective-C语法之KVC的使用
- Objective-C语法之KVO的使用
- IT English Collection(11)of Object graph
- IOS深入学习(11)之Object graph