Re: threads in Xcode
Re: threads in Xcode
- Subject: Re: threads in Xcode
- From: Chris Hanson <email@hidden>
- Date: Fri, 16 Jul 2004 20:53:51 -0700
On Jul 16, 2004, at 4:26 PM, Mai Bui wrote:
[NSThread detachNewThreadSelector: @selector(RunLoop:) toTarget:self
withObject:nil];
In the above line, you're passing the selector "RunLoop:" but later in
your program the method is named "RunLoop". The colon is a
syntactically and semantically significant part of the method name;
"RunLoop:" and "RunLoop" are different selectors.
Also, just a note on style: Methods in Objective-C should always start
with a lower-case letter, unless they start with an acronym (e.g.
+[NSURL URLFromString:]).
Finally, the above line will create a new thread and invoke [self
RunLoop:nil] in it. It will not pause execution of the current thread,
so the following lines:
NSLog(@"Endof run\n");
[self performSelectorOnMainThread:@selector(hasStopped)
withObject:NULL waitUntilDone:false];
will be executed almost immediately.
- (void) RunLoop
{
int i;
NSString *str = @"print out the string\n";
for (i=0; i<1500; i++)
{
[statusText insertText:str]; // use insertText since statusText is
NSTextView class
There are restrictions on what you can do safely from non-main threads
in the AppKit. Here's a pointer to the latest documentation covering
it all:
<
http://developer.apple.com/documentation/Cocoa/Conceptual/
Multithreading/>. Pay special attention to the section "Using the
Application Kit from Multiple Threads".
- (IBAction)stopRun:(id)sender
{
[NSThread exit];
This will be executed in your main thread, since the main thread is the
one that handles events from the human interface. You also generally
shouldn't exit a thread this way anyway. Instead, the thread should
notice -- say through an event posted on its run loop, or by checking a
shared data structure (access to which must be appropriately
synchronized, of course) -- that it must exit, and just fall through
the end of the method that was invoked by
+detachNewThreadSelector:toTarget:withObject:.
-- Chris
--
Chris Hanson <email@hidden>
http://www.livejournal.com/users/chanson/
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.