• 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: threads in Xcode
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.


  • Follow-Ups:
    • Re: threads in Xcode
      • From: Mai Bui <email@hidden>
References: 
 >threads in Xcode (From: Mai Bui <email@hidden>)

  • Prev by Date: Re: Cocoa Bindings Question(s)
  • Next by Date: Re: Cocoa Bindings Question(s)
  • Previous by thread: threads in Xcode
  • Next by thread: Re: threads in Xcode
  • Index(es):
    • Date
    • Thread