On Jun 13, 2011, at 2:07 PM, Daniel Luis dos Santos wrote:

Hello,

I have a GUI application built with interface builder. One of the windows has a NSTextView in it. I connected it to an object also referenced in the interface builder file.
The goal is to write to the NSTextView from a background trhead that does some task. I launch the thread with a call to

NSThread.alloc().initWithTarget_selector_object_()

The behaviour I am getting is erratic. Most times it blocks on the call to the NSTextView's setString_() method and the application hangs. If I run the code on the main thread it runs fine.

How do I do this from a background thread ? The idea is to keep the application responsive.

GUI and multi-theading = bad idea.

The main or GUI-thread is very sensitive about state being changed from other threads while it does it's works.

So usually there are ways to remedy this by special events that get pushed into the GUI-threads event-queue, or similar means.

The documentation relevant for this should be

 http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Multithreading/RunLoopManagement/RunLoopManagement.html#//apple_ref/doc/uid/10000057i-CH16-SW44


That should get you started.

Diez