Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iphone-线程没有停止,为什么它在我的程序后台运行。?_Iphone_Nstimer_Nsthread - Fatal编程技术网

iphone-线程没有停止,为什么它在我的程序后台运行。?

iphone-线程没有停止,为什么它在我的程序后台运行。?,iphone,nstimer,nsthread,Iphone,Nstimer,Nsthread,我的程序中有一个线程。它由NSTimer运行。当我停止NSThread或NSTimer时。它停止了我的线程。但是当我想再次运行程序中的线程时,它会告诉我,前一个线程并没有停止。所以前一个线程和当前线程一起运行。我的程序如下 - (void) startTimer{ timerThread = [[NSThread alloc] initWithTarget:self s

我的程序中有一个线程。它由NSTimer运行。当我停止NSThread或NSTimer时。它停止了我的线程。但是当我想再次运行程序中的线程时,它会告诉我,前一个线程并没有停止。所以前一个线程和当前线程一起运行。我的程序如下

- (void) startTimer{
    timerThread = [[NSThread alloc] 
                             initWithTarget:self 
                             selector:@selector(startTimerThread) object:nil];

    [timerThread start];
}

-(void) startTimerThread{
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
    timer = [[NSTimer scheduledTimerWithTimeInterval: 2.0
                                      target: self
                                    selector: @selector(timerTick:)
                                    userInfo: nil
                                     repeats: YES] retain];

    [runLoop run];
    [pool release];
}

- (void)stopTimer{

    [timer invalidate];
    [timer release];
    timer = nil;
}

- (void)timerTick:(NSTimer *)timer
{
    NSLog(@"THik");
}
我的程序有两个按钮,一个用于启动线程,另一个用于停止线程。第一次运行和停止线程时,它工作正常。但是第二次(停止线程后)当我再次想要运行线程时,它告诉我,前一个线程没有停止,两个线程同时运行。请帮帮我


如何停止一个线程,当我想再次运行该线程时,它会运行一个新线程,而不是前一个线程。

您只是在stopTimer方法中停止计时器。线程仍在执行。 要停止线程,可以执行
[NSThread exit]

这将停止“当前”线程

要了解有关停止线程的更多信息,请参考其类引用并检查“exit”方法


因此,如果在主线程上调用
[NSThread exit]
,它将退出主线程。

嘿,这里是停止计时器而不是线程。就线程而言,您可以运行多个线程,而不管它们的方法体是什么。i、 e.
startTimerThread
可以有多个线程,您需要的是创建新线程或只运行上一个线程。?