举例说明怎么简单的创建一个子线程
2013-08-19 18:22
363 查看
函数setupThread:(NSArray*)userInfor。通过userInfor将需要的数据传到线程中。
函数定义:
-(void)setupThread:(NSArray*)userInfor{
[NSThread detachNewThreadSelector:@selector(threadFunc:)
toTarget:self withObject:(id)userInfor];
}
- (void)threadFunc:(id)userInfor{
NSAutoreleasePool*pool
= [[NSAutoreleasePool alloc]
init];
//。。。。需要做的处理。
//这里线程结束后立即返回
[self
performSelectorOnMainThread:@selector(endThread) withObject:nil
waitUntilDone:NO];
[pool
release];
}
performSelectorOnMainThread通知主线程执行函数endThread。也可以使用performSelector:onThread:withObject:waitUntil
通知某线程执行线程结束后的处理。
线程内不要刷新界面。如果需要刷新界面,通过performSelectorOnMainThread,调出主线程中的方法去刷新。
例如,启动一个线程下载图片:
//启动线程
[NSThread
detachNewThreadSelector:@selector(downloadImage:) toTarget:self
withObject:url];
//线程函数
- (void) downloadImage:(NSString*)url{
_subThreed =
[NSThread currentThread];
self.uploadPool = [[NSAutoreleasePool alloc]
init];
self.characterBuffer = [NSMutableData data];
done =
NO;
[[NSURLCache
sharedURLCache] removeAllCachedResponses];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURLURLWithString:url]];
self.connection = [[NSURLConnection alloc]
initWithRequest:theRequest delegate:self];
[self
performSelectorOnMainThread:@selector(httpConnectStart)
withObject:nil waitUntilDone:NO];
if
(connection != nil) {
do {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
beforeDate:[NSDate distantFuture]];
} while (!done);
}
self.photo =
[UIImage imageWithData:characterBuffer];
//下载结束,刷新
[self
performSelectorOnMainThread:@selector(fillPhoto) withObject:nil
waitUntilDone:NO];
// Release resources used only in this thread.
self.connection = nil;
[uploadPool
release];
self.uploadPool = nil;
_subThreed =
nil;
}
#pragma mark NSURLConnection Delegate
methods
/*
Disable caching so that each time we run this app
we are starting with a clean slate. You may not want to do this in
your application.
*/
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
willCacheResponse:(NSCachedURLResponse *)cachedResponse {
return
nil;
}
// Forward errors to the
delegate.
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error {
done =
YES;
[self
performSelectorOnMainThread:@selector(httpConnectEnd)
withObject:nil waitUntilDone:NO];
[characterBuffer setLength:0];
}
// Called when a chunk of data
has been downloaded.
- (void)connection:(NSURLConnection *)connection
didReceiveData:(NSData *)data {
// Process the downloaded chunk of data.
[characterBuffer appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[self
performSelectorOnMainThread:@selector(httpConnectEnd)
withObject:nil waitUntilDone:NO];
// Set the condition which ends the run loop.
done =
YES;
}
相关文章推荐
- C# 创建线程的简单方式:异步委托
- SQLite3创建和简单使用说明
- 【Java 线程的深入研究3】最简单实例说明wait、notify、notifyAll的使用方法
- 用Pthread创建线程的一个简单Demo
- Executors常用的创建ExecutorService的方法的简单说明
- 安卓程序子线程创建和简单使用学习
- Html中JS脚本执行顺序简单举例说明
- Windows线程(一)最简单的线程创建
- Python_多线程1(创建线程,简单线程同步)
- java + 线程系列之showThread(一)----简单的线程创建方式有两种
- 进程与线程的一个简单解释(目前看到的关于进程和线程说明最好的博文)
- KEIL下创建工程示例及配置简单说明
- Html中JS脚本执行顺序简单举例说明
- 程序,进程,线程的简单说明
- 【Linux】一个简单的线程创建和同步的例子
- 回调函数详解 简单举例说明
- 界面和线程交互之简单使用说明
- 简单使用BackgroundWorker创建多个线程的教程
- Android异步通信机制简单地理解,Handler是当前线程的消息队列中的一个子队列,而Runable是可以被安排到Handler去运行的接口。 首先在Activity中创建一个继承自Han
- C# 多线程编程基础——怎么创建和执行多个线程