Re: To run a block of code at every 1 second
Re: To run a block of code at every 1 second
- Subject: Re: To run a block of code at every 1 second
- From: Charles Srstka <email@hidden>
- Date: Tue, 3 Mar 2009 14:36:31 -0600
On Mar 3, 2009, at 11:17 AM, Nick Rogers wrote:
Hi,
Here's the code:
the following method is running in a separate thread:
- (void)myFucntion
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
for (;;)// for loop starts here and is very quick, 100s of
iterations per second
{
{
// this block of code needs to be run at around 1 second interval
// code here
[appController
performSelectorOnMainThread:@selector(updateProgress:)
withObject:data waitUntilDone:YES];
}
// other loop code here
}
[loop release];
}
I have tried some crude methods for implementing this running of
block of code at 1 second interval but they don't perform as
expected, for that I used running a separate thread setting a BOOL
var to YES at every 1 second.
Is there a better way to do it?
If the only purpose of using a separate thread here was to run the
timer, you can skip the threading altogether, and just do this
somewhere:
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self selector:@selector(myFunction) userInfo:nil repeats:YES];
Then, in myFunction:
- (void)myFunction
{
// put the stuff that went inside your for loop in the original
function here
}
Much simpler and easier.
Charles
_______________________________________________
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