Use of multiple threads in iPhone development and precautions (1)

Source: Internet
Author: User

IPhoneDevelopmentMultithreadingUsage and precautions: This article describes how to use multithreading in the iPhone SDK and precautions. Although most PC applications currently supportMultithreading/Multi-task development method, but inIPhoneOn, Apple is not recommendedMultithreadingProgramming Method.

HoweverMultithreadingAfter all, programming is a development trend and is said to be coming soon.IPhoneOS4 will be fully supportedMultithreading. So MasterMultithreadingProgramming method, in some cases can be dug outIPhoneMore potential.

Start with an example

Start with a routine. For more information about the code, see here. You can download other routines.

For the control model of multi-threaded programs, see here. Generally, the manager/worker model is used. Here, we use NSThread in the iPhone SDK to implement it.

First, create a new View-based application project named "TutorialProject ". As shown in the interface, use UILabel to implement the two parts (Thread Part and Test Part). The Thread Part contains a UIProgressView and a UIButton. The Test Part contains a value and a UISlider,

Next, create IBOutlets for each UI control in the TutorialProjectViewController. h file.

 
 
  1. @interface TutorialProjectViewController : UIViewController {  
  2.     // ------ Tutorial code starts here ------  
  3.     // Thread part  
  4.     IBOutlet UILabel *threadValueLabel;  
  5.     IBOutlet UIProgressView *threadProgressView;  
  6.     IBOutlet UIButton *threadStartButton;  
  7.     // Test part  
  8.     IBOutlet UILabel *testValueLabel;  
  9.     // ------ Tutorial code ends here ------  

At the same time, you also need to create the property of the outlets variable.

 
 
  1. @property (nonatomic, retain) IBOutlet UILabel *threadValueLabel;  
  2. @property (nonatomic, retain) IBOutlet UIProgressView *threadProgressView;  
  3. @property (nonatomic, retain) IBOutlet UIProgressView *threadStartButton;  
  4. @property (nonatomic, retain) IBOutlet UILabel *testValueLabel; 

Next, define the action function when the button is pressed and the variable function of slider.

 
 
  1. - (IBAction) startThreadButtonPressed:(UIButton *)sender;   
  2. - (IBAction) testValueSliderChanged:(UISlider *)sender; 

Then in the TutorialProjectViewController. m file synthesize outlets, and in the file to implement dealloc release resources.

 
 
  1. @synthesize threadValueLabel, threadProgressView, testValueLabel, threadStartButton;  
  2. ...  
  3. - (void)dealloc {  
  4.     // ------ Tutorial code starts here ------  
  5.     [threadValueLabel release];  
  6.     [threadProgressView release];  
  7.     [threadStartButton release];  
  8.     [testValueLabel release];  
  9.     // ------ Tutorial code ends here ------  
  10.     [super dealloc];  

Start nowThreadFirst, when the thread button is pressed, create a newThread.

 
 
  1. - (IBAction) startThreadButtonPressed:(UIButton *)sender {  
  2.     threadStartButton.hidden = YES;  
  3.     threadValueLabel.text = @"0";  
  4.     threadProgressView.progress = 0.0;  
  5.     [NSThread detachNewThreadSelector:@selector(startTheBackgroundJob) toTarget:self withObject:nil];  

After the button is pressed, hide the button to prevent multiple threads from being created. Then initialize the display value and progress bar, and create a new thread. The thread function is startTheBackgroundJob.

The specific startTheBackgroundJob function is defined as follows.

 
 
  1. -(Void) startTheBackgroundJob {
  2. NSAID utoreleasepool * pool = [[NSAID utoreleasepool alloc] init];
  3. // Pause the thread for 3 seconds after it starts. Here, we only demonstrate the method for pausing the thread. You do not have to do this)
  4. [NSThread sleepForTimeInterval: 3];
  5. [Self defined mselecw.mainthread: @ selector (makeMyProgressBarMoving) withObject: nil waitUntilDone: NO];
  6. [Pool release];
  7. }

In row 1st, An NSAID utoreleasepool object is created to manageThreadAutomatically released object Resources. Here, the NSAID utoreleasepool isThreadReleased upon exit. This complies with the general rules of the Cocoa GUI application.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.