starting cocoa in different thread
starting cocoa in different thread
- Subject: starting cocoa in different thread
- From: Robert Nikander <email@hidden>
- Date: Thu, 25 Oct 2007 14:23:54 -0400
Hi,
For reasons I won't go into (unless you are interested) I want to
start Cocoa on a thread that is not the main posix thread. I wrote
the small test program below that creates a little window with a
button in it. There is a variable 'BOOL use_thread' in the main
function. If it is 'NO', then the program runs fine -- I can can
click the button, see that events are being generated. If use_thread
= YES, then it doesn't work -- sendEvent: gets called constantly with
null NSEvent pointers.
I thought I would be able to use any pthread as the "main" Cocoa
event thread, as long as that thread called [NSApplication
sharedApplication], set up the UI, and called [NSApp run]. Is that
possible? If so, how?
thank you,
Rob
file: two_threads.m
compiled with: gcc -o two_threads two_threads.m -framework Cocoa
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#include <pthread.h>
static int eventCount = 0;
@interface MyApp : NSApplication
@end
@implementation MyApp
{
}
- (void)sendEvent:(NSEvent*)e
{
printf("MyApp.sendEvent: %p %d %s\n",
e, ++eventCount,
[[e description] UTF8String]);
[super sendEvent: e];
}
@end
static void multithread_cocoa()
{
// Put Cocoa into multithreaded mode by starting an NSThread
that does nothing.
id obj = [[NSObject alloc] init];
SEL m = @selector(isEqual:);
[NSThread detachNewThreadSelector:m toTarget:obj withObject:obj];
}
void *run_cocoa(void *ig)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSApplication *app = [MyApp sharedApplication];
id window = [[NSWindow alloc] initWithContentRect: NSMakeRect
(10,10,100,100)
styleMask: NSTitledWindowMask |
NSResizableWindowMask | NSClosableWindowMask |
NSMiniaturizableWindowMask
backing: NSBackingStoreBuffered
defer: NO];
[window makeKeyAndOrderFront: window];
id b = [NSButton new];
[b setTitle: @"Hi"];
NSRect r = NSMakeRect(0,0,100,100);
[b setFrame: r];
[[window contentView] addSubview: b];
[app run];
[pool release];
}
int main(int argc, char **argv)
{
BOOL use_thread = YES;
if (use_thread)
{
multithread_cocoa();
pthread_t t;
pthread_create(&t, NULL, run_cocoa, NULL);
pthread_join(t, NULL);
}
else
{
run_cocoa(NULL);
}
return 0;
}
_______________________________________________
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