我是iOS编程新手。我使用以下代码 ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property dispatch_queue_t background;
- (IBAction)stop_resume:(UIBarButtonItem *)sender;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize background;
- (void)viewDidLoad
{
[super viewDidLoad];
background = dispatch_queue_create("background", NULL);
dispatch_async(background, ^{
for(int i=0; ; i++)
{
NSLog(@"Number is %d\n", i);
}
});
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (IBAction)stop_resume:(UIBarButtonItem *)sender
{
dispatch_suspend(background);
}
@end
当我按下按钮后,后台线程不会被挂起,循环继续运行。