简体   繁体   English

如何有效地对AppKit进行独立处理?

[英]How to effectively do independent processes to the AppKit?

I have been told, "AppKit controls don't take kindly to being updated from threads other than the main thread." 有人告诉我,“ AppKit控件不善于从主线程以外的其他线程进行更新。”

I have 2 processes: 我有2个流程:

- update variables in the heap periodically
- display values from the heap to the display

I did do: 我确实做了:

NSThread *thread_Client = [[NSThread alloc] initWithTarget:self selector:@selector(myTcpClient) object:nil];
NSThread *thread_Display = [[NSThread alloc] initWithTarget:self selector:@selector(displayData) object:nil];

but after awhile it blanks out or locks up. 但过一会儿它会消失或锁定。

What is a better approach? 有什么更好的方法?

thx 谢谢

I'm a bit unclear about what you're trying to do. 对于您要做什么我还是不太清楚。 It sounds like you are trying to perform a cyclical operation in a background thread and (at the end of each cycle) use the results to update the UI. 听起来您正在尝试在后台线程中执行循环操作,并且(在每个周期结束时)使用结果来更新UI。 Other postings suggest using an NSTimer in the main thread (which is the only thread that you should use for updating the UI). 其他帖子建议在主线程中使用NSTimer(这是用于更新UI的唯一线程)。 You would have the timer fire every so often (repeatedly); 您会经常(反复)触发计时器; in the callback that is invoked when it fires, you would copy whatever data/info from the object(s) being updated by the background thread into appropriate UI views and then invoke setNeedsDisplay to cause updating of the UI. 在触发时调用的回调中,您需要将后台线程正在更新的对象中的所有数据/信息复制到适当的UI视图中,然后调用setNeedsDisplay来引起UI的更新。 An issue is the consistency of the info you are using: if the background thread continues to update object(s), you could copy a mix of old and new info into the UI. 问题是您使用的信息的一致性:如果后台线程继续更新对象,则可以将新旧信息混合复制到UI中。 Perhaps you need a 'delivery' object that the background fills in and then passes to the UI updater; 也许您需要一个“交付”对象,背景将填充该对象,然后传递给UI更新程序; such an object would contain a consistent set of info as yielded at the end of the background cycle. 这样的对象将包含在后台循环结束时产生的一致信息集。

In any case you do not want to be sleeping in the main thread. 无论如何,您都不希望在主线程中睡眠。 All it does is slow down the UI response. 它所做的只是降低UI响应速度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在iPhone中成功导入Appkit Framework? - How to successfully import Appkit Framework in iPhone? iOS进程如何获得他们的名字? - How do iOS processes get their name? Objective-C,Doxygen以及如何在文档中显示AppKit类 - Objective-C, Doxygen and how to show AppKit classes in documentation 如何实现超时/等待NSStream以有效地使方法同步 - How do I implement a timeout / wait for an NSStream to effectively make method synchronous 如何有效地使用MVC架构 - How to use MVC architecture effectively 如何在spritekit中有效地暂停游戏? - How to effectively pause a game in spritekit? 如何处理iOS ViewController中的异步进程? - How do I handle asynchronous processes in iOS ViewControllers? 如何在不担心索引的情况下添加到NSDictionary? 有效地每个索引包含多个元素的NSArray。 有更好的解决方案吗? - How do I add to NSDictionary without worrying about the indices? Effectively an NSArray with multiple elements per index. Is there a better solution? 输入 <AppKit/AppKit.h> 不起作用 - Importing <AppKit/AppKit.h> does not work 如何在NSNumber和NSString对象之间有效转换 - How to effectively convert between NSNumber and NSString objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM