如何取消或停止 NSThread?

发布于 2024-12-04 08:12:51 字数 726 浏览 3 评论 0原文

我正在做一个应用程序,在读取 XML 文件时使用 NSThread 加载 viewControllers 的内容。

我已经完成如下:

-(void)viewDidAppear:(BOOL)animated
{
    // Some code...


    [NSThread detachNewThreadSelector:@selector(loadXML) toTarget:self withObject:nil];
    [super viewDidAppear:YES];
}

-(void)loadXML{

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    // Read XML, create objects...

    [pool release];
}

我的问题是,如果用户在加载 NSThread 时更改到另一个 viewController,我不知道如何停止 NSThread,从而导致应用程序崩溃。

我尝试按如下方式取消或退出 NSThread 但没有成功:

-(void)viewsDidDisappear:(BOOL)animated{
    [NSThread cancel];
    // or [NSThread exit];
    [super viewDidDisappear:YES];
}

任何人都可以帮忙吗?谢谢。

I'm doing an app that loads the contents of viewControllers using NSThread while is reading an XML file.

I have it done as follows:

-(void)viewDidAppear:(BOOL)animated
{
    // Some code...


    [NSThread detachNewThreadSelector:@selector(loadXML) toTarget:self withObject:nil];
    [super viewDidAppear:YES];
}

-(void)loadXML{

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    // Read XML, create objects...

    [pool release];
}

My problem is that I don't know how to stop the NSThread if the user changes to another viewController while the NSThread is loading, doing that the app crashes.

I've tried to cancel or exit the NSThread as follows but without success:

-(void)viewsDidDisappear:(BOOL)animated{
    [NSThread cancel];
    // or [NSThread exit];
    [super viewDidDisappear:YES];
}

Can anyone help? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

幻想少年梦 2024-12-11 08:12:51

当您分离新线程时,您无法再从 viewDidDisappear 等取消或退出它。这些 UI 特定方法仅在主线程上执行,因此退出/取消适用于主线程,这显然是错误的。

不要使用分离新线程方法,而是在 .h 中声明 NSThread 变量,并使用 initWithTarget:selector: object: 方法初始化它,并随时随地取消它。 。

When you detach new thread, you can no more cancel or exit it from viewDidDisappear etc. These UI specific methods execute only on main thread so the exit/cancel applies to the main thread which is obviously wrong.

Instead of using the detach new thread method, declare NSThread variable in .h and initialize it using initWithTarget: selector: object: method and cancel it whenever/wherever you want to..

安稳善良 2024-12-11 08:12:51

您还可以使用NSThread[NSThread exit];方法。

you can also use [NSThread exit]; method of NSThread.

又怨 2024-12-11 08:12:51

如果可以的话,最好让线程优雅地结束,即自然结束。听起来你的情况是你负担得起的。另请确保您是从主线程而不是辅助线程更新用户界面,因为 UIKit 不是线程安全的。

It's better to let a thread end gracefully, i.e. reach its natural conclusion, if you can. It sounds like in your case you can afford to. Also be sure that you're updating the user interface from the main thread, not a secondary thread, as UIKit is not thread safe.

埋葬我深情 2024-12-11 08:12:51

你写道:
...线程完成时应用程序停止响应...

一旦您将线程标记为取消或退出,您就必须手动停止调用该线程执行的任何操作。一个例子:
....

- (void) doCalculation{
/* Do your calculation here */
}
- (void) calculationThreadEntry{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSUInteger counter = 0;
while ([[NSThread currentThread] isCancelled] == NO){

[self doCalculation];
    counter++;
    if (counter >= 1000){ break;
    } }
    [pool release]; }
    application:(UIApplication *)application
    - (BOOL)
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    /* Start the thread */
    [NSThread detachNewThreadSelector:@selector(calculationThreadEntry)
    toTarget:self withObject:nil];
    // Override point for customization after application launch. [self.window makeKeyAndVisible];
    return YES;
    }

在此示例中,循环的条件是线程处于非取消状态。

You wrote:
... the app stops responding while the thread finishes...

Once you flag a thread for cancelling or exit, you have to manually stop whatever the thread was called to do. An example:
....

- (void) doCalculation{
/* Do your calculation here */
}
- (void) calculationThreadEntry{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSUInteger counter = 0;
while ([[NSThread currentThread] isCancelled] == NO){

[self doCalculation];
    counter++;
    if (counter >= 1000){ break;
    } }
    [pool release]; }
    application:(UIApplication *)application
    - (BOOL)
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    /* Start the thread */
    [NSThread detachNewThreadSelector:@selector(calculationThreadEntry)
    toTarget:self withObject:nil];
    // Override point for customization after application launch. [self.window makeKeyAndVisible];
    return YES;
    }

In this example, the loop is conditioned on the thread being in a non-cancelled state.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文