Re: Threading Question
Re: Threading Question
- Subject: Re: Threading Question
- From: Daniel Jalkut <email@hidden>
- Date: Tue, 25 Oct 2005 00:21:37 -0400
Jonah: Why don't you define your threaded function so that it gets
passed whatever it needs to communicate with. E.g.:
[NSThread detachNewThreadSelector:@selector(doThreadedThing:)
toTarget:myWorkerObject withObject:self];
Inside the class for "myWorkerObject" you will have a function
something like this:
- (void) doThreadedThing:(id)concernedObject
{
// Do my thing
...
// Notify my concerned object...
[concernedObject performSelectorOnMainThread:@selector
(workerObjectCompletedThing:) withObject:self waitUntilDone:YES];
}
That seems much cleaner than trying to define some global variable
line of communications. Hope this helps,
Daniel
On Oct 24, 2005, at 10:40 PM, Jonah Horowitz wrote:
That seems reasonable, my problem is that I'm not sure how to call
performSelectorOnMainThread... I need a pointer to the main thread,
which I don't have. I can have the main thread call a function after
detachNewThread... which sets the value of a thread variable, like so:
- (void)setGlobalController:(MainController *)controller
{
globalController = controller;
}
globalController being defined in my worker.h as
MainController* globalController;
but this just doesn't seem like it's the right way to do it.
Jonah
--- Ryan Britton <email@hidden> wrote:
Try performselectorOnMainThread:withObject:waitUntilDone:
http://developer.apple.com/documentation/Cocoa/Reference/Foundation/
ObjC_classic/Classes/NSObject.html#//apple_ref/doc/uid/20000050-
CJBEHAEF
Just have the delegate method call a finishing method using that.
On Oct 24, 2005, at 6:56 PM, Jonah Horowitz wrote:
I've been going over the documentation on threads under cocoa.
I've
looked at both the SimpleThreads and TrivialThreads sample
projects.
I have one main "controller" thread that manages everything and it
spawns two worker threads using the "+connectWithPorts" function in
the
sample code.
I've pasted the snippet here:
+ (void)connectWithPorts:(NSArray *)portArray
{
NSLog(@"ConnectWithPorts Called thread id = %d",[NSThread
currentThread]);
NSAutoreleasePool *pool;
Getter *getterObject;
NSConnection *connectionToStufferController;
pool = [[NSAutoreleasePool alloc] init];
connectionToStufferController = [NSConnection
connectionWithReceivePort:[portArray objectAtIndex:3]
sendPort:[portArray objectAtIndex:2]];
getterObject = [[self alloc] init];
[[portArray objectAtIndex:6] unlock];
[ ((StufferController *)[connectionToStufferController
rootProxy])
setGetterServer:getterObject];
[getterObject release];
[[NSRunLoop currentRunLoop] run];
[pool release];
return;
}
I've had no problems whatsoever calling functions in my worker
threads,
but I can't figure out how to communicate from my worker threads
back
to the controller thread when the controller didn't initiate the
coversation.
I'm using NSURLDownload to retrieve a file and when it completes
the
download it calls a downloadDidFinish delegate in my worker thread.
How can my worker thread call my controller thread to let it know
that
the download is complete.
I've tried moving the NSConnection up out of the connectWithPorts
and
into the class implementation, but that seems to crash the program
when
connectWithPorts is called.
I'm totally lost and I've been banging at this for hours. Any help
would be greatly appreciated.
Thanks,
Jonah Horowitz
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
sweater.com
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden