GCD退出线程执行
2015-10-07 13:25
411 查看
GCD本身并没有停止正在运行的线程的API,但可以通过添加线程时进行循环判断调用,如果停止的标志为ture则停止线程运行。方法如下:
点击导航栏上的退出则退出线程通讯:
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"开始" style:UIBarButtonItemStylePlain target:self action:@selector(testAction)];
点击导航栏上的退出则退出线程通讯:
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"开始" style:UIBarButtonItemStylePlain target:self action:@selector(testAction)];
- (void)testAction { NSLog(@"执行了testAction的按钮"); [self closeThread]; [self.navigationController popViewControllerAnimated:YES]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; q = dispatch_get_global_queue(0, 0); dispatch_async(q, ^{ while (![self shouldCancel]) { NSLog(@"query times "); sleep(2); }; }); } - (void)closeThread { if (q) { self.shouldCancel = YES; dispatch_suspend(q); dispatch_release(q); q = nil; } }
相关文章推荐
- 一次关闭多个Activity
- fork
- 读取文件中的数据
- 分片集群
- Android 5.1 上运行Oprofile (虚拟机与nexus
- iOS--关于AFN和ASI的post登录请求
- hdu 2896 病毒侵袭 AC自动机
- 硬链接和软链接的区别与作用
- IOS开发笔记-01按钮操作-12.frame&bounds&center属性
- 欢迎使用CSDN-markdown编辑器
- 程序运行时遵循的UIApplicationDelegate协议
- Java集合框架面试问题集锦
- 对象属性拷贝工具类的性能比较
- 欢迎使用CSDN-markdown编辑器
- 为openfiler添加第二块网卡
- UITableView reload 单个 cell 的数据
- 相关算法在游戏中的应用
- VS2012设置Js和Jquery提示
- 库的概念及制作
- Exchange2016正式版功能介绍及全新安装配置