Re: Works in main Thread, but not in background Thread (modified)
Re: Works in main Thread, but not in background Thread (modified)
- Subject: Re: Works in main Thread, but not in background Thread (modified)
- From: glenn andreas <email@hidden>
- Date: Wed, 07 Jan 2009 09:29:47 -0600
On Jan 7, 2009, at 9:01 AM, John Love wrote:
NSWorkspace *workSpace = [NSWorkspace sharedWorkspace];
NSArray *runningAppDictionaries = [workSpace
launchedApplications];
Let me wrap this up by saying when myRoutine is called in my main
thread, everything works fine .. but when called by my background
thread (via [NSThread detachNewThreadSelector:toTarget:withObject:]
it definitely does not work.
An interesting side note is that if I insert the following as the
very 1st line in myRoutine:
return [self myOtherRoutine];
it starts working in the background thread. If this sounds "crazy"
you are definitely correct. Once again, "some strange problem
somewhere".
NSWorkspace is not documented as being thread safe, so you should only
access it from the main thread. One can envision two possible places
where it is failing to work in a background thread:
[NSWorkspace sharedWorkspace] isn't thread safe (and potentially
doesn't allocate the singleton when not on the main thread, so will
return nil) - by calling your routine from the main thread,
NSWorkspace is is properly allocated and set up so later calls will
work.
launchedApplications isn't thread safe - again, it may need to set
some things up that only work on the main thread, and so returns
something incorrect in the background, but by calling it first from
your main thread these values get correctly updated.
The problem is that you don't know which case is true (and it is
probably some other answer - these are just random speculations to
show the uncertainty of the operation), so even though it works if you
do it the first time from the main thread, you don't know if that will
work on all of your customers' systems (and will continue to work when
OS upgrades happen). Good thread-safe programs are paranoid - assume
that the system is written in a way as to make any of your assumptions
false at some time, and code defensively.
Glenn Andreas email@hidden
<http://www.gandreas.com/> wicked fun!
JSXObjC | the easy way to unite JavaScript and Objective C
_______________________________________________
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