2

嗨,我想用另一个线程启动一个计时器,并想在那个线程中重复这个过程。我试过但它不起作用。

我的代码片段如下

 [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];       
}

谁能建议我一个正确的方法来做到这一点?

提前致谢。

4

2 回答 2

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
于 2013-04-29T11:02:41.097 回答
1

试试这个代码:

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 

[正常运行时间无效];

于 2013-04-29T10:39:28.447 回答