gpt4 book ai didi

iphone - 如何运行另一个线程并使用该线程运行计时器来重复进程?

转载 作者:行者123 更新时间:2023-12-03 20:51:19 27 4
gpt4 key购买 nike

嗨,我想用另一个线程启动一个计时器,并想在该线程中重复该过程。我试过了,但没用。

下面给出了我的代码片段

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

有人可以建议我一个正确的方法吗?

提前致谢。

最佳答案

#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

关于iphone - 如何运行另一个线程并使用该线程运行计时器来重复进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16276300/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com