IOS 线程处理 子线程的启动与结束

技术交流新QQ群:414971585
 

IOS中,如果要在主线程中启动一个子线程,可以又两种方法:

[NSThread detachNewThreadSelector:@selector(myThreadMainMethod:) toTarget:self withObject:nil]; 
这是在cocoa早期提供的方法,因此你可以在任何版本的ios和mac上调用此方法。
在 OS X v10.5(or later)和IOS中,苹果又提供了一种方法,可以允许你获得你的thread句柄,并且更方便的让主线程控制子线程。

NSThread* myThread = [[NSThread alloc] initWithTarget:self 
                                        selector:@selector(myThreadMainMethod:) 
                                        object:nil]; 
[myThread start];  // Actually create the thread

如果要停止子线程,有两种方法:
第一种,是在子线程中执行:

[NSThread exit];

另一种是在主线程执行:

[myThread cancel];  
要注意的是,[mThread cancel]; 并不能exit线程,只是标记为canceled,但线程并没有死掉。加入你在子线程中执行了一个循环,则cancel后,循环还在继续,你需要在循环的条件判断中加入 !mThread.isCancelled 来判断子线程是否已经被cancel来决定是否继续循环。

下面是我的一个测试demo,可以参考一下:

@synthesize mThread; 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
     
    NSLog(@"main thread:%@",[NSThread currentThread]); 
     mThread=[[NSThread alloc] initWithTarget:self selector:@selector(subThreadMethod) object:nil]; 
    [NSThread detachNewThreadSelector:@selector(performMethod) toTarget:self withObject:nil]; 
  
} 
-(void)subThreadMethod{ 
    int i=1; 
    while (i++>0 && ![[NSThread currentThread]isCancelled]) { 
        NSLog(@"subthread i:%d ,thread:%@",i,[NSThread currentThread]); 
    }   
} 
 
- (IBAction)startThread:(id)sender { 
    NSLog(@"startThread...."); 
    [mThread start]; 
} 
 
- (IBAction)stopThread:(id)sender { 
    NSLog(@"mThread.isCancelled: %d",mThread.isCancelled); 
    if (!mThread.isCancelled) { 
        [mThread cancel]; 
//        [mThread exit]; //exit 是类方法,不可以用在对象上 
    } 
} 
 
- (IBAction)performOnSubThread:(id)sender { 
    //在子线程调用方法 
     [self performSelector:@selector(performMethod) onThread:mThread withObject:nil waitUntilDone:NO]; 
} 
-(void)performMethod{ 
    NSLog(@"performMethod.... thread:%@",[NSThread currentThread]);     
} 
@end

转自 http://www.cnblogs.com/ygm900/archive/2013/05/26/3100076.html

IOS 线程处理 子线程的更多相关文章

  1. iOS中使用子线程的完整方法

    http://www.cnblogs.com/ygm900/archive/2013/06/23/3151691.html 第一步:开启子线程 //开启子线程到网络上获取数据 myFirstThrea ...

  2. C# WebService中任务处理线程创建子线程后

    protected void WriteLog(string message) { lock (lockObject) { var file = System.IO.File.AppendText(& ...

  3. Android 使用handler实现线程间发送消息 (主线程 与 子线程之间)、(子线程 与 子线程之间)

    keyword:Android 使用handler实现线程间发送消息 (主线程 与 子线程之间).(子线程 与 子线程之间) 相信大家平时都有使用到异步线程往主线程(UI线程)发送消息的情况. 本文主 ...

  4. C#创建子线程,子线程使用委托更新控件

    一.背景 由于在窗体程序中通过点击一个button按键后需要更新TreeView控件的内容,由于等待时间比较长,主程序无法一起在那边等待,需要去处理其它的事情,所以就需要创建新的子线程来处理.因为主线 ...

  5. iOS 报错:(子线程中更新UI)This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread. This can lead to engine corruption and weird crashes.

    今天在写程序的时候,使用Xcode 运行工程时报出下面的错误错信息,我还以为是什么呢,好久没遇到过这样的错误了. **ProjectName[1512:778965] This application ...

  6. C# 多线程详解 Part.02(UI 线程和子线程的互动、ProgressBar 的异步调用)

           我们先来看一段运行时会抛出 InvalidOperationException 异常的代码段: private void btnThreadA_Click(object sender, ...

  7. GUI(主)线程与子线程之间的通信(用信号槽通讯)

    在主线程上,可以控制子线程启动,停止,清零 如果子线程启动的话,每一秒钟会向主线程发送一个数字,让主线程更新界面上的数字. 程序截图: 上代码: #include <QtGui> #inc ...

  8. QT GUI(主)线程与子线程之间的通信——使用跨线程的信号槽

    在主线程上,可以控制子线程启动,停止,清零 如果子线程启动的话,每一秒钟会向主线程发送一个数字,让主线程更新界面上的数字. 程序截图: 上代码: #include <QtGui> #inc ...

  9. Java Thread.join()详解--父线程等待子线程结束后再结束

    目录(?)[+] 阅读目录 一.使用方式. 二.为什么要用join()方法 三.join方法的作用 join 四.用实例来理解 打印结果: 打印结果: 五.从源码看join()方法   join是Th ...

随机推荐

  1. c++作业:Circle

    Circle Github链接

  2. 前端:IE兼容性的相关方法

    有一段时间做前端的时候,IE下的就兼容性是比较令人头痛的问题,我在这一过程中也是看了很多的资料,然后把一些自己觉得比较普遍的问题进行一下相关的总结. 1.在IE6下,格式为png的图片在IE6上的透明 ...

  3. 用jQuery Mobile做HTML5移动应用的三个优缺点

    JQuery Mobile 和 HTML5 的 3个优点 1. 上手迅速并支持快速迭代:在一个星期多一点的时间里,通过阅读JQuery Mobile文档以及O’Reilly出版的JQuery Mobi ...

  4. nginx url重定向

    nginx内部支持url rewrite,内部编译进去了rewrite模块,nginx的rewrite模块类似于apache的rewriterule功能:支持多种规则和正则表达式: 详细介绍如下: N ...

  5. JQ对象到底是什么

    jQuery对象是什么,举个例子,$('#id') 返回的就是jQuery对象,这个东西是整个jQuery的核心所在,所以我先来分析它. var jQuery = function( selector ...

  6. VMware10 安装centos6.7 设置NAT模式

    最近刚开始学Linux运维.我看的书是<跟阿铭学Linux>,视频教程里面使用NAT模式手动分配IP可以成功ping通网关,但是我照着视频一步一步操作却一直不成功,不知道是什么原因,昨天弄 ...

  7. C 计算数组长度

    int data[4],length; length=sizeof(data)/sizeof(data[0]);  //数组占内存总空间除以单个元素占内存空间大小,即等于元素个数 printf(&qu ...

  8. 一个Java线程小例子(仿火车票售卖)

    public class MyThread extends Thread{ private static int ticket=100; public void run(){ for(int i=0; ...

  9. LeetCode 102

    方法一:(迭代) /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * ...

  10. Longest Increasing Path in a Matrix -- LeetCode 329

    Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...