IOS 线程处理 子线程
IOS 线程处理 子线程的启动与结束
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 线程处理 子线程的更多相关文章
- iOS中使用子线程的完整方法
http://www.cnblogs.com/ygm900/archive/2013/06/23/3151691.html 第一步:开启子线程 //开启子线程到网络上获取数据 myFirstThrea ...
- C# WebService中任务处理线程创建子线程后
protected void WriteLog(string message) { lock (lockObject) { var file = System.IO.File.AppendText(& ...
- Android 使用handler实现线程间发送消息 (主线程 与 子线程之间)、(子线程 与 子线程之间)
keyword:Android 使用handler实现线程间发送消息 (主线程 与 子线程之间).(子线程 与 子线程之间) 相信大家平时都有使用到异步线程往主线程(UI线程)发送消息的情况. 本文主 ...
- C#创建子线程,子线程使用委托更新控件
一.背景 由于在窗体程序中通过点击一个button按键后需要更新TreeView控件的内容,由于等待时间比较长,主程序无法一起在那边等待,需要去处理其它的事情,所以就需要创建新的子线程来处理.因为主线 ...
- 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 ...
- C# 多线程详解 Part.02(UI 线程和子线程的互动、ProgressBar 的异步调用)
我们先来看一段运行时会抛出 InvalidOperationException 异常的代码段: private void btnThreadA_Click(object sender, ...
- GUI(主)线程与子线程之间的通信(用信号槽通讯)
在主线程上,可以控制子线程启动,停止,清零 如果子线程启动的话,每一秒钟会向主线程发送一个数字,让主线程更新界面上的数字. 程序截图: 上代码: #include <QtGui> #inc ...
- QT GUI(主)线程与子线程之间的通信——使用跨线程的信号槽
在主线程上,可以控制子线程启动,停止,清零 如果子线程启动的话,每一秒钟会向主线程发送一个数字,让主线程更新界面上的数字. 程序截图: 上代码: #include <QtGui> #inc ...
- Java Thread.join()详解--父线程等待子线程结束后再结束
目录(?)[+] 阅读目录 一.使用方式. 二.为什么要用join()方法 三.join方法的作用 join 四.用实例来理解 打印结果: 打印结果: 五.从源码看join()方法 join是Th ...
随机推荐
- Android 振动器
今天介绍一下Android的振动器Vibrator,有三个方法来控制手机振动: 1.void vibrate(long milliseconds):控制手机振动milliseconds毫秒. 2.vo ...
- SQLSERVER排查CPU占用高的情况
SQLSERVER排查CPU占用高的情况 今天中午,有朋友叫我帮他看一下数据库,操作系统是Windows2008R2 ,数据库是SQL2008R2 64位 64G内存,16核CPU 硬件配置还是比较高 ...
- Learning in Two-Player Matrix Games
3.2 Nash Equilibria in Two-Player Matrix Games For a two-player matrix game, we can set up a matrix ...
- Android 中dp和px
dp是虚拟像素,在不同的像素密度的设备上会自动适配,比如: 在320x480分辨率,像素密度为160,1dp=1px 在480x800分辨率,像素密度为240,1dp=1.5px 计算公式: 1dp* ...
- 【zz】matlab 直方图匹配
原文地址:http://www.cnblogs.com/tiandsp/archive/2012/12/19/2825418.html 直方图匹配或叫做直方图规定化都可以,是把原图像的直方图按照给定的 ...
- JS总结 运算符 条件语句
算术运算符+-*/ 与数学计算一致,配合()号进行各种计算 另外,+号还可以达到连接的作用.例如: var a = 15; alert("a的值等于"+a); 前后增减量运算符: ...
- JAVA Arrays.binarySearch
转自:http://blog.csdn.net/somebodydie/article/details/8229343 package com.jaky; import java.util.*; pu ...
- 将DataTable导出到Excel
/// <summary> /// 导出Excel /// </summary> /// <param name="dtData"></p ...
- 数据结构《21》----2014 WAP 初试题----Immutable queue
2014 WAP初试题----实现一个不可变的队列: 看似很简单..实则,不同的版本效率的差距可能是巨大的..甚至难以想象.. 之前用STL库的queue进行了对比,差别非常大.. 用上一篇文章的im ...
- C# 字符串操作类
using System; using System.Collections.Generic; using System.Text; using System.Collections; using S ...