辅助线程关于

发布于 2024-10-09 13:42:10 字数 1079 浏览 0 评论 0原文

我的应用程序中有一个主线程和一个辅助线程。众所周知,主线程正在被 UI 使用。我使用辅助线程来执行视图的后台加载。 我有一个主控制器,我称之为虚拟控制器。从那里我调用我的主控制器,它处于横向模式。

所以基本上我要做的是,当我旋转虚拟控制器时,我希望能够加载我的主景观控制器,反之亦然。

当我从虚拟旋转到主时,一切正常。 主控制器有一系列要加载的子控制器,因此我已将该进程放在辅助线程上,到那时它们正在加载,我会显示加载器屏幕。

dummy 是一个普通的视图控制器,main 是一个模态视图控制器。

现在问题来了。

如果用户尝试旋转虚拟模型 --> main,旋转方法启动主视图控制器的初始化。但由于某种原因,用户改变了主意并从 main-->dummy 状态转回,理想情况下,由于 UI 线程(主线程)是空闲的,因此转换应该是平滑的。发生的情况是 UI 旋转回来。但由于主控制器上的辅助线程仍然处于打开状态,即使在主线程在 MainController 上调用了关闭模态控制器之后,当我们调用 willRotate 函数时。

我在各种帖子中读到我们不能简单地杀死辅助线程,因为我们必须自己清理东西。那么任何人都可以指导我完成代码,我如何杀死我的辅助线程并在我转回我的调用表单之前进行清理..? 它有点紧急......请不要指向其他线程主题,因为我已经访问过这些主题。

我特别想知道如何停止我的辅助线程,而不导致崩溃。 谢谢。

-(void)viewDidLoad {

[super viewDidLoad];

workingThread = [[NSThread alloc]initWithTarget:self selector:@selector(parseMainTextFile) object:nil];

[workingThread start];

[NSThread detachNewThreadSelector:@selector(addLoader) toTarget:self withObject:nil];
    //[self performSelectorOnMainThread:@selector(parseMainMatchTextFile) withObject:nil waitUntilDone:NO];

}

I have a main and an auxiliary thread in my app. Main thread as everyone knows is being used by UI. I use the secondary thread to do the background loading of my views.
I have a main controller which i call a dummy controller. From there i call my main controller, which is in landscape mode.

So basically what i have to do is, when i rotate my dummy controller, i want to be able to load my main landscape controller and vice versa.

Everything works fine when i rotate from dummy to main.
The main has a series of child controllers to load, so i have put up that process on a secondary thread, and by that time they are loading i show a loader screen.

The dummy is a normal view controller, and the main is a modal view controller.

Now the problem.

if a user is trying to rotate form dummy --> main, the rotate method kicks of the initialisation of the main view controller. But for some reason the user changes his mind and rotates back form main-->dummy ideally, the transition should be smooth as the UI thread(main theread) is free. What happens is the UI rotates back. But since the secondary thread on the Main controller is still on, even after the dismiss modal controller is called on the MainController by the main thread, when we call the willRotate function.

I've read in various post that we cannot simply kill the secondary thread, as we have to do clean up stuff ourselves. So can anyone please guide me through code, how do i kill my secondary thread and do clean up before i transit back to my calling form..??
Its kinda urgent... please dont point to other thread topics, cos i've already visited those.

I specifically want to know how should i stop my secondary thread, without causing a crash.
thanks.

-(void)viewDidLoad {

[super viewDidLoad];

workingThread = [[NSThread alloc]initWithTarget:self selector:@selector(parseMainTextFile) object:nil];

[workingThread start];

[NSThread detachNewThreadSelector:@selector(addLoader) toTarget:self withObject:nil];
    //[self performSelectorOnMainThread:@selector(parseMainMatchTextFile) withObject:nil waitUntilDone:NO];

}

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

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

发布评论

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

评论(1

沧桑㈠ 2024-10-16 13:42:10

一般来说,如果您运行后台线程,则意味着您有一个运行循环,它在 while 语句中运行。您应该有一个停止运行循环的标志。这样它将把线程的状态转换为完成。

while (mRunLoop && [mLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]);

我就是这样做的。另外,请阅读此帖子及其评论中的子帖子。祝你好运!

Generally saying, if you run a background thread it means you've got a Run Loop, which works in a while statement. You should have a flag which stops the Run Loop. this way it would transform the state of the thread to finished.

while (mRunLoop && [mLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]);

This is the way I do. Additionally read this Post and its sub posts from comments. Good luck!

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