Parse NSThread to create the Run Loop in the iPhone application

Source: Internet
Author: User

InIPhoneApplication in progressNSThreadCreateRun LoopIt is the content to be introduced in this article, although the iphone provides us with many simple and easy-to-operate thread methods.IPhoneMulti-threaded programming is proposed to use NSOperation and NSOperationQueue, which is really useful. However, in some cases, we still need to use the originalNSThread. This requiresNSThreadCreate a run loop.

 
 
  1. NSThread * thread1 = [[NSThread alloc] initWithTarget: self selector: @ selector (playerThread:) object: nil];
  2. [Thread start];
  3. // If you want to use NSOperation, the principle is similar. You just need to add it to the queue .. Queue will call the method at the right time. The following code serves as a reference.
  4. -(Void) playerThread: (void *) unused
  5. {
  6. AudioRunLoop = CFRunLoopGetCurrent (); // The runloop reference of the subthread
  7. NSAID utoreleasepool * pool = [[NSAID utoreleasepool alloc] init ]; //
  8. Run loop [self initPlayer]; CFRunLoopRun (); //
  9. Run loop. This will stop. [Pool release];
  10. }
  11. // Implement a timer to check the working status of the sub-thread and perform task switching when appropriate. Or stop yourself when appropriate.
  12. Run loop-(void) initPlayer {
  13. // Here you can initialize a work class, such as sound or video playback.
  14. NSTimer * stateChange = [NSTimer scheduledTimerWithTimeInterval: 0.5 target: self selector:
  15. @ Selector (checkStatuserInfo: nil repeats: YES];
  16. }
  17. -(Void) checkState :( nstmer *) timer
  18. {
  19. If (self-thread exited ){
  20. // Release resources in the Child thread
  21. CFRunLoopStop (CFRunLoopGetCurrent (); // end the subthread task.
  22. }
  23. }

Summary:IPhoneApplication in progressNSThreadCreateRun loopI hope this article will help you!

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.