2

Hi i want to start a timer with another thread and want to repeat process in that thread. i had tried but it doesn't work.

my code snippet is given below

 [NSThread detachNewThreadSelector:@selector(setupTimerThread) toTarget:self   withObject:nil];


    -(void)setupTimerThread
    {
    NSLog(@"inside setup timer thread");
    // self.startTiemr = [[NSTimer alloc]init];
    startTiemr = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self    selector:@selector(triggerTimer) userInfo:nil repeats:YES];

    runLoop = [NSRunLoop currentRunLoop];
     [runLoop addTimer:startTiemr forMode:NSRunLoopCommonModes];
        [runLoop run];       
}

can anyone suggest me a proper way to do that?

Thanks in advance.

Muddu Patil
  • 713
  • 1
  • 11
  • 24
jpd
  • 381
  • 1
  • 2
  • 13
  • have a look the below link http://stackoverflow.com/questions/7055424/ios-start-background-thread – Pradeep Apr 29 '13 at 10:30
  • thanks for your attention but i want to start a thread that run a timer and timer will repeat after 30 seconds – jpd Apr 29 '13 at 10:40

2 Answers2

2
#import "TimePassViewController.h"

@interface TimePassViewController ()

@end

@implementation TimePassViewController
{
    NSTimer *timer;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

   // [self performSelectorOnMainThread:@selector(yourmethod) withObject:nil waitUntilDone:NO];


    NSThread* myThread = [[NSThread alloc] initWithTarget:self
                                                 selector:@selector(yourmethod)
                                                   object:nil];
    [myThread start];  // Actually create the thread

}





-(void)yourmethod
{
    @autoreleasepool
    {
        NSRunLoop *TimerRunLoop = [NSRunLoop currentRunLoop];
        timer =  [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];
        [TimerRunLoop run];
    }
   //timer =  [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];
}

-(void)timerMethod
{
    NSLog(@"aaaaaaaaaaaa");
    //[timer invalidate];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
Pradeep
  • 1,043
  • 7
  • 12
  • thanks for your answer but i want to run it on a separate thread not on main thread – jpd Apr 29 '13 at 11:16
1

try this code:

NStimer  *uptimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(bachground_todaylist) userInfo:nil repeats:YES];

 -(void)bachground_todaylist
    {

        [self performSelectorInBackground:@selector(yourmethod) withObject:nil];


    }
you wants to stop proces 

[uptimer invalidate];

Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144
kirti Chavda
  • 3,029
  • 2
  • 17
  • 29
  • performselectorinbackground will stop after all (yourmethod) done? – jpd Apr 29 '13 at 10:58
  • no only stop uptimer invalidate if you wants to stop then set timer repeat NO – kirti Chavda Apr 29 '13 at 10:59
  • but if it repeats after every 1 second each time new thread created – jpd Apr 30 '13 at 05:12
  • i want to create a single another thread and repeats its process after every 20 seconds and when i click stop button it should stop.and in this process after every 20 seconds webservice will called but in this process application should not jamm on that view controller – jpd Apr 30 '13 at 05:20
  • jamm only view is refresh or reload – kirti Chavda Apr 30 '13 at 05:24
  • when url request will start sending and getting response view will not allow to go to another view and another function – jpd Apr 30 '13 at 06:29
  • try this method :[NSThread detachNewThreadSelector:@selector(method) toTarget:self withObject:nil]; remove performSelectorInBackground method – kirti Chavda Apr 30 '13 at 06:34
  • thanks for reply, i used this method but it runs as earlier methods – jpd Apr 30 '13 at 06:39