• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Getting the value of GUI elements from non-main threads
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Getting the value of GUI elements from non-main threads


  • Subject: Re: Getting the value of GUI elements from non-main threads
  • From: Cem Karan <email@hidden>
  • Date: Wed, 4 Feb 2009 16:39:26 -0500

On Feb 4, 2009, at 4:10 PM, Ken Thomases wrote:

On Feb 4, 2009, at 2:47 PM, Cem Karan wrote:

I know that we can only modify GUI elements from the main thread, but can we get the values for objects from other than the main thread? E.g., assume I have an editable NSTextField, and I have a worker thread. The user edits the field, clicks a button, and the main thread hands off the analysis to a worker thread. The worker thread then directly queries the NSTextField via its stringValue method. Is this kosher?

No. First, NSTextField isn't on the list of thread-safe classes.

OK, this is the part that I need to worry about, as I'm concerned about crashes.


But quite aside from that, even if it were safe, you could not be sure the value you would get is the value you should get. It might have changed since the task was started.

In the scenario you describe, the button action method should marshal all of the inputs for the worker unit that's to be processed on the worker thread. Those inputs should be passed as integral parts of the work unit to the worker thread.

Fully and wholeheartedly agreed; I should have spent more time explaining. In my case, it doesn't even matter if the worker thread gets the correct value, or a value at all, as this is some fast and dirty test code that I'm going to use once and never look at again. However, you're right, even for something like that (maybe especially for something like that) its best to do it correctly. It just means I need to rewrite my real code a little so that testing can go more smoothly, something I was hoping to avoid doing.


Thanks,
Cem Karan
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Getting the value of GUI elements from non-main threads (From: Cem Karan <email@hidden>)
 >Re: Getting the value of GUI elements from non-main threads (From: Ken Thomases <email@hidden>)

  • Prev by Date: Re: Getting the value of GUI elements from non-main threads
  • Next by Date: Re: [Q] Control over the "You cannot use the application ... with this version of Mac OS X" string
  • Previous by thread: Re: Getting the value of GUI elements from non-main threads
  • Next by thread: Update an existing Core Data store when the model changes?
  • Index(es):
    • Date
    • Thread