Re: NSThread Question
Re: NSThread Question
- Subject: Re: NSThread Question
- From: Lorenzo Puleo <email@hidden>
- Date: Wed, 24 Apr 2002 00:18:19 +0200
on 4/23/02 8:19 PM, Drew McCormack at email@hidden wrote:
>
On Tuesday, April 23, 2002, at 07:45 PM, Lorenzo Puleo wrote:
>
>
> I should have to run/stop a cycle using the same button.
>
>
>
> I would like to break the loop at any time pressing the same button.
>
> But when I press the button to start the loop, I can't press again the
>
> button to stop the loop. I have to wait the loop ends...
>
> Also, the button still appears blue-pressed...
>
>
>
> I understand I have to use NSThread, but I don't understand how I can do
>
> this using my current routine since NSThread requires methods (+) which
>
> don't allow to manipulate variable instances and factories within
>
> theirselves as, instead, I have to do.
>
>
>
> Any ideas would be greatly appreciated as I am relatively
>
> new to Cocoa. Thanks.
>
>
I found Threadworker was a good way to break off a thread easily. It
>
also has good example code for you to look at.
>
Check it out:
>
http://iharder.sourceforge.net/macosx/threadworker/
>
>
By the way, I think you are wrong about not being able to access
>
instance variables. When you call the NSThread
>
factory method, you pass a target in, which is the object for which the
>
thread will execute a method. You can of course make the target "self"
>
if you want to run your loop and be able to access the ivar. You may
>
also need to use NSLock to prevent synchronization problems though (see
>
docs).
>
>
Drew
>
>
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
>
... ... ... ... ... ... ... ...
>
Dr. Drew McCormack
>
Trade Strategist (www.trade-strategist.com)
>
Trading simulation software for Mac OS X
>
Thank you for your help. The example code you suggested won't compile with
my PB 1.1.1. Anyway I have found a line of that sample very useful, so I
made this (it works very fine! - If you think some error could occur with
this code in the future, please advise):
- (IBAction)executeLoop
{
if(isRunning == YES){
// isRunning is a variable instance of this class
isRunning = NO;
return;
}
isRunning = YES;
[NSThread detachNewThreadSelector:@selector(processTheItems)
toTarget:self withObject:nil];
}
- (IBAction)processTheItems
{
int i;
// gItemsToDoArray is a variable instance of this class
for(i = 0; i<[gItemsToDoArray count]; i++){
if(isRunning == NO) break;
[self processSingleItem:[gItemsToDoArray objectAtIndex:i]];
}
// This shows an alert telling how much items
// were been processed
[self showReport:i];
}
Thanks Drew!
--
Lorenzo Puleo
mailto:email@hidden
_______________________________________________
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.