passing data to threads
passing data to threads
- Subject: passing data to threads
- From: "William Irving Zumwalt" <email@hidden>
- Date: Tue, 19 Dec 2006 22:39:12 -0600
I'm trying to implement threads and not sure how to pass the *config object
into a newly created thread. I'm using an example from the book Cocoa
Programming.
Anyone have ideas?
--- MyController.m
- (IBAction)startNet:(id)sender {
// I'm not sure how to pass this *config into the thread ...
NetworkConfig *config = [networkOutline activeConfig];
[MyNetwork startNetworkThreadWithTag:0 forController:self];
[networkServer start];
}
MyNetwork is subclassed from the NetworkObject interface ...
--- NetworkObject.h
@protocol NetworkMethods
- (void)startNetwork;
- (void)togglePauseNetwork;
- (void)pauseNetwork;
- (void)resumeNetwork;
- (void)stopNetwork;
@end
@protocol NetworkController
- (void)setNetwork:(id)anObject tag:(int)serverTag;
- (void)setNetworkProgress:(double)newStatus
finished:(BOOL)running tag:(int)tag;
@end
@interface NetworkObject : NSObject <NetworkMethods>
{
int tag;
BOOL paused, running;
id <NetworkController> parent;
}
+ (NSConnection *)startNetworkThreadWithTag:(int)tag
forController:(id <NetworkController>)controller;
- (id)initForParent:(id <NetworkController>)theParent withTag:(int)theTag;
@end
Here is the NetworkObject implementation which uses ports, but I'm not sure
how to make use of them. I have a feeling it's here I should be passing the
data to.
@interface NetworkObject(_private)
+ (void)_connectWithPorts:(NSArray *)portArray;
@end
@implementation NetworkObject
+ (NSConnection *)startNetworkThreadWithTag:(int)theTag
forController:(id <NetworkController>)controller
{
NSPort *port1 = [NSPort port];
NSPort *port2 = [NSPort port];
NSArray *portArray = [NSArray arrayWithObjects:port2, port1,
[NSNumber numberWithInt:theTag], nil];
NSConnection *networkConnection = [[NSConnection alloc]
initWithReceivePort:port1 sendPort:port2];
[networkConnection setRootObject:controller];
[NSThread detachNewThreadSelector:@selector(_connectWithPorts:)
toTarget:self withObject:portArray];
return networkConnection;
}
+ (void)_connectWithPorts:(NSArray *)portArray
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSConnection *networkConnection = [NSConnection
connectionWithReceivePort:[portArray objectAtIndex:0]
sendPort:[portArray objectAtIndex:1]];
int theTag = [[portArray objectAtIndex:2] intValue];
id rootProxy = (id)[networkConnection rootProxy];
NetworkObject *networkObject = [[self alloc] initForParent:rootProxy
withTag:theTag];
[rootProxy setNetwork:networkObject tag:theTag];
[networkObject release];
{
NSMutableDictionary *threadDictionary =
[[NSThread currentThread] threadDictionary];
NSLog(@"Thread dictionary:\n%@\n", [threadDictionary description]);
}
[[NSRunLoop currentRunLoop] run];
[pool release];
}
- (id)init
{
return [self initForParent:nil withTag:0];
}
- (id)initForParent:(id <NetworkController>)theParent withTag:(int)theTag
{
self = [super init];
if (!self) {
return nil;
}
tag = theTag;
parent = theParent;
running = NO;
paused = NO;
return self;
}
- (void)start
{
[self testMethod];
}
Any help much appreciated.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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