1

I am new to iOS programming. I am using the following code 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

When i press the button the background thread does not get suspended, the loop continues to run.

Indrajeet
  • 5,490
  • 2
  • 28
  • 43
Sumit Kumar
  • 402
  • 1
  • 6
  • 26
  • Think about what `dispatch_suspend()` is suspending. It's not suspending the thread, it's suspending the dispatch of what's in the queue to be run on the thread. – Kitsune Mar 18 '14 at 05:15
  • then please help me, how can i suspend the running thread? – Sumit Kumar Mar 18 '14 at 05:30
  • What is it that you're actually trying to accomplish? Could you refactor your code so it places the work as individual items on a queue, rather than starting a single block that performs all the work? Or perhaps the code could periodically check the state of a shared variable to see if it should stop working? – Kitsune Mar 18 '14 at 14:44

0 Answers0