• 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: To run a block of code at every 1 second
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: To run a block of code at every 1 second


  • Subject: Re: To run a block of code at every 1 second
  • From: Michael Vannorsdel <email@hidden>
  • Date: Tue, 3 Mar 2009 10:33:56 -0700

Make the main thread method something like this:

- (void)myThreadStart
{
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(myFucntion) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] run]; //Run loop needs to be running to fire the timer


	[pool release];
}

This will call myFucntion about every second. If the thread it too busy for the next firing, it will try to fire again at the next second interval.

As a side note, be careful setting globals like BOOL from various threads. Use the OSAtomic* operations to make it thread safe.


On Mar 3, 2009, at 10:17 AM, Nick Rogers wrote:

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?

_______________________________________________

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


References: 
 >To run a block of code at every 1 second (From: Nick Rogers <email@hidden>)

  • Prev by Date: Latent objects remain associated in NSTokenField after they are deleted/removed?
  • Next by Date: To run a block of code at every 1 second
  • Previous by thread: Re: To run a block of code at every 1 second
  • Next by thread: Re: To run a block of code at every 1 second
  • Index(es):
    • Date
    • Thread