Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > NSThread

NSThread
Thread Tools
Admin Emeritus
Join Date: Oct 2000
Location: Boston, MA
Status: Offline
Reply With Quote
Jan 25, 2001, 05:20 PM
 
Does anyone have code, or an example on how I'd implement an NSThread?

Thanks.
"Against stupidity, the gods themselves contend in vain" (Schiller)
     
Mac Enthusiast
Join Date: Feb 2000
Location: Storrs,Connecticut, USA
Status: Offline
Reply With Quote
Jan 26, 2001, 04:48 PM
 
/* Okay here is a bit of code to demonstrate threads in Cocoa. These two methods would be in the same class. input is an outlet to an NSTextField in the interface, output is an outlet to a non-editable NSTextView. Make OKButton something that is called when an OK button next to the input field is pressed. All this does is it takes input and put it into output 60 times, 1 time per second. If this wasn't multithreaded then you wouldn't be able to minimize or hide the window or choose any menu items during the 60 second period that this thing is displaying the output.*/


- (IBAction)OKButton
{
[NSApplication detachDrawingThread: @selector (doSomething
toTarget: self
withObject: [[input stringValue] retain]];
}

- (void)doSomethingNSString *)inputString
{
NSAutoreleasePool* pool;
NSRange range;
int length,
i;

range.location = 0;
range.length = [inputString length];

for(i = 0;i < 60; i++)
{
pool = [[NSAutoreleasePool alloc] init];
[output replaceCharactersInRange: range
withString: inputString];

range.location += length;
[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];
[pool release];
}
[inputString release];
}

[This message has been edited by Dalgo (edited 01-26-2001).]
     
Admin Emeritus
Join Date: Oct 2000
Location: Boston, MA
Status: Offline
Reply With Quote
Jan 26, 2001, 08:12 PM
 
You're awsome. Thanks :-)

Now, what does the whole pool thing do? It works without any of that release/pool code, but does that cause wasted memory or something?
"Against stupidity, the gods themselves contend in vain" (Schiller)
     
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
Jan 27, 2001, 04:19 AM
 
AHHH!
you obviously haven't read ObjC.pdf or whatever it is... It explains all the autorelease pools and things. it's important.
     
Admin Emeritus
Join Date: Oct 2000
Location: Boston, MA
Status: Offline
Reply With Quote
Jan 27, 2001, 08:36 AM
 
No, I haven't :-(

I'm just *way* too excited with Cocoa.

***calms down.

OK, I'll read the documentation this weekend.
"Against stupidity, the gods themselves contend in vain" (Schiller)
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 12:11 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2