Works in main Thread, but not in background Thread (modified)
Works in main Thread, but not in background Thread (modified)
- Subject: Works in main Thread, but not in background Thread (modified)
- From: John Love <email@hidden>
- Date: Wed, 7 Jan 2009 10:01:58 -0500
This request focuses on an old cocoabuilder thread:
http://www.cocoabuilder.com/archive/message/cocoa/2007/3/8/179989
David writes:
>>>>>
But now I find I have a BOOL method that is rarely and randomly
returning NO when the only exit to the method is "return YES;" so I
have some strange problem somewhere. It turned out that the method
didn't need to be BOOL, but the result is checked anyway.
>>>>>
Instead of his BOOL, I have an INT in the form of an ENUMerated
constant. However, unlike David, I still have not yet discovered my
version of "some strange problem somewhere".
Code follows:
typedef int myStatus;
enum {
kNoError,
kConstant1,
kConstant2,
kWhoops
};
- (myStatus) myRoutine {
if ([self myOtherRoutine] == kNoError) {
// calculations here
if (something) return kConstant1;
else return kConstant2;
}
else {
return kWhoops;
}
}
Here's myOther routine:
- (myStatus) myOtherRoutine {
NSWorkspace *workSpace = [NSWorkspace sharedWorkspace];
NSArray *runningAppDictionaries = [workSpace
launchedApplications];
NSString *keyName, *keyValue;
keyName = @"NSApplicationBundleIdentifier";
keyValue = @"com.microsoft.Excel";
NSDictionary *aDictionary;
BOOL foundExcel;
for (aDictionary in runningAppDictionaries) {
foundExcel = [[aDictionary valueForKey:keyName]
isEqualToString:keyValue];
if (foundExcel) break;
}
// if (!foundExcel) NSLog(@"no Excel");
return (foundExcel ? kNoError : kWhoops);
}
NSLog prints "no Excel" many times and stops (as it should when
foundExcel becomes true). So everything is okay so far .. until I
forcibly quit Excel "Cmd-Tab-Q". When I do this quit, NSLog should
print "no Excel", but it does not!
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".
Thanks for listening.
_______________________________________________
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