我可以开始由$ P $一个线程在可可接口pssing一个按钮,继续使用界面,同时线程运行? [英] Can I start a thread by pressing a button in a cocoa interface and keep using interface while thread runs?

查看:79
本文介绍了我可以开始由$ P $一个线程在可可接口pssing一个按钮,继续使用界面,同时线程运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可可界面。当我preSS一个按钮,我想处理一些数据,但我想用的接口,而它的工作,以保持。我想唯一的解决办法是NSThread。现在会不会有一个锁定装置,$ P $从一个IBAction为方法返回,如果它产生一个线程pventing我?

I have a Cocoa interface. When I press a button I want to process some data, but I want to keep using the interface while it's working. I guess the only solution is NSThread. Now will there be a locking mechanism preventing me from returning from an IBAction method if it spawns a thread?

推荐答案

看看的NSOperation 的NSOperation 是必须被继承它是有用的几类可可之一。通过添加委托属性为你的的NSOperation 子类的操作完成时,你可以得到通知。此外,您还可以添加一个 USERINFO 属性,以允许操作arbitary数据传递回委托

Take a look at NSOperation. NSOperation is one of the few cocoa classes which must be subclassed for it to be useful. By adding a delegate property to your NSOperation subclass you can get notified when the operation completes. Also, you can add a userInfo property to allow the operation to pass back arbitary data to the delegate

@implementation MyNSOperationSubclass

-(void)main
{
    //do operation here



    //operationResult is used to report back to the delegate. operationResult could include a userInfo key so that the delegate can have some data passed back, or an error key to indicate success of the operation.
    NSDictionary *operationResult; 


    //Some checks to ensure that the delegate implements operationHasFinished: should be added.
    //waitUntilDone: YES locks the main thread
    [[self delegate] performSelectorOnMainThread:@selector(operationHasFinished:)     withObject:operationResult waitUntilDone: YES];

}

@end

这篇关于我可以开始由$ P $一个线程在可可接口pssing一个按钮,继续使用界面,同时线程运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆