Re: Scheduling a selector when a thread completes
Re: Scheduling a selector when a thread completes
- Subject: Re: Scheduling a selector when a thread completes
- From: Marc Van Olmen <email@hidden>
- Date: Wed, 29 Apr 2009 22:04:43 -0400
* You could use NSOperationQueue and set the maximumThread count to 1.
and make for each of these methods an NSOperation. And then add those
4 operations to your OperationQueue.
* But your case is simple so you can easily code this your self by
creating an object for each operation you are performing. and create
an Operation Array
yourself.
cheers,
marc
On Apr 29, 2009, at 9:55 PM, email@hidden wrote:
Hi
I wrote a directory scanner application that spawns 4 different
threads with independent rescan intervals and need to repeat the
process if and only if a thread has completed a scan. Basically, for
each file type I'm watching, the process is
1. Spawn a thread to sample files of the specified type
2. Wait until the thread completes
3. Schedule another pass after a given time interval has passed.
Timer's don't work for my purposes because the execution time of the
thread can easily exceed any arbitrary repeat interval I might
choose which would require the use of busy flags and testing for a
busy state etc... I want to avoid that.
I tried calling NSObject's
performSelector:(SEL) aSelector withObject:(id) anArgument
afterDelay:( NSTimeInterval ) delay
But the receiver's " aSelector" method is never getting called. The
only logical place to perform this scheduling is from inside the
thread because only the thread knows when it is finished, but I'm
having no luck getting the method to run.
Here are the methods that spawn the threads
- ( void ) initDirectories
{
[ NSThread detachNewThreadSelector : @selector
( initDirectoriesInThread :)
toTarget : self
withObject : nil ];
}
- ( void ) updateDirectories
{
// Logging function that writes messages to my app's custom
console view
KCLog ([ NSString stringWithFormat : @"Checking for changes:
%@" , userType]);
[ NSThread detachNewThreadSelector : @selector
( updateDirectoriesInThread :)
toTarget : self
withObject : nil ];
}
And here are the corresponding thread methods
- ( void ) initDirectoriesInThread:( id ) inData
{
@synchronized ( self )
{
// allocate a new autorelease pool
NSAutoreleasePool *pool = [[ NSAutoreleasePool alloc ] init ];
KDirectory *rootDirectory = [ KDirectory directoryWithPath :
path ];
rootObserver = [[ CNCDirectoryWatcher alloc ]
initWithDirectory : rootDirectory root : self ];
// add root directory to observed list
[ self addDirectory : rootObserver ];
// schedule a new pass
[ self performSelector : @selector ( updateDirectories )
withObject : nil afterDelay : catalogInterval ];
// release the pool
[pool release ];
}
}
- ( void ) updateDirectoriesInThread:( id ) inData
{
@synchronized ( self )
{
// allocate a new autorelease pool
NSAutoreleasePool *pool = [[ NSAutoreleasePool alloc ] init ];
NSArray *dirs = [ directories copy ];
NSEnumerator *enumerator = [dirs objectEnumerator ];
CNCDirectoryWatcher *directory;
while (directory = [enumerator nextObject ])
{
[directory update ];
}
// schedule a new pass
[ self performSelector : @selector ( updateDirectories )
withObject : nil afterDelay : catalogInterval ];
// release the pool
[pool release ];
}
}
The "initDirectoriesInThread" method is getting called and runs
correctly, but the "updateDirectoriesInThread" method never gets
called.
I looked at
-(void)performSelectorOnMainThread:(SEL) aSelector withObject:(id)
arg waitUntilDone:(BOOL) wait
But it doesn't appear to have a way to schedule the execution for a
specified delay.
Is there a way to use performSelector: withObject: afterDelay: to
satisfy both step 2 and 3 above?
Thanks for any help
_______________________________________________
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
_______________________________________________
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