舉例說明怎麼簡單的建立乙個子執行緒。
用到的類是nsthread類,這裡使用detachnewtheadselector:totagaet:withobject建立乙個執行緒。
函式setupthread:(nsarray*)userinfor。通過userinfor將需要的資料傳到執行緒中。
函式定義:
-(void )setupthread:(nsarray*)userinfor
- (void )threadfunc:(id)userinfor
performselectoronmainthread通知主線程執行函式endthread。也可以使用performselector:onthread:withobject:waituntil 通知某執行緒執行執行緒結束後的處理。
執行緒內不要重新整理介面。如果需要重新整理介面,通過performselectoronmainthread,調出主線程中的方法去重新整理。
//啟動執行緒
[nsthread detachnewthreadselector:@selector(downloadimage:) totarget:self withobject:url];
//執行緒函式
- (void) downloadimage:(nsstring *)url 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
- (nscachedurlresponse *)connection:(nsurlconnection *)connection willcacheresponse:(nscachedurlresponse *)cachedresponse
// forward errors to the delegate.
- (void)connection:(nsurlconnection *)connection didfailwitherror:(nserror *)error
// called when a chunk of data has been downloaded.
- (void)connection:(nsurlconnection *)connection didreceivedata:(nsdata *)data
iphone開發 多執行緒
from 舉例說明怎麼簡單的建立乙個子執行緒。用到的類是nsthread類,這裡使用detachnewtheadselector totagaet withobject建立乙個執行緒。函式setupthread nsarray userinfor。通過userinfor將需要的資料傳到執行緒中。函式...
iphone開發 多執行緒
舉例說明怎麼簡單的建立乙個子執行緒。用到的類是nsthread類,這裡使用detachnewtheadselector totagaet withobject建立乙個執行緒。函式setupthread nsarray userinfor。通過userinfor將需要的資料傳到執行緒中。函式定義 vo...
iphone開發 多執行緒
舉例說明怎麼簡單的建立乙個子執行緒。用到的類是nsthread類,這裡使用detachnewtheadselector totagaet withobject建立乙個執行緒。函式setupthread nsarray userinfor。通過userinfor將需要的資料傳到執行緒中。函式定義 vo...