osdir.com
mailing list archive F.A.Q. -since 2001!



Subject: Re: Cocoa, Java and threads - msg#00183

List: macosx.devel

Mail Archive Navigation:
by Date: Prev Next Date Index by Thread: Prev Next Thread Index

To communicate back to the main thread, you can use NSEvent's otherEvent class method to create a new event of type ApplicationDefined. Then you can use NSApplication's postEvent instance method to add this to the application's event queue (which, according to the documentation, works from subthreads). Finally, you can subclass NSApplication or NSWindow to override their sendEvent instance method and do any special processing when your event is received (such as updating your interface).

It's kind of roundabout, I know. I'm surprised Apple doesn't support -performSelectorOnMainThread:withObject:waitUntilDone: from Cocoa-Java; it's probably even more necessary there than it is in Objective-C, since Java programmers tend to write code with lots of threads.

Hmm, another strategy you could probably use: Create a simple Objective-C class that just turns around and does the -performSelector... for you.

@interface ThreadForwarder : NSObject
{ }
+ (void)tellObject:(id)object performSelectorOnMainThread:(SEL)aSelector
withObject:(id)arg waitUntilDone:(BOOL)wait;
@end

@implementation ThreadForwarder
+ (void)tellObject:(id)object performSelectorOnMainThread:(SEL)aSelector
withObject:(id)arg waitUntilDone:(BOOL)wait
{
[object performSelectorOnMainThread:aSelector
withObject:arg
waitUntilDone:wait];
}
@end

Then bridge that class to Java, and use it from your Cocoa-Java code. It looks a little hackish, but not all that terrible.

(Note: I haven't tried this and won't warrant that it will work. But it might be worth a look.)

-- Chris

--
Chris Hanson, bDistributed.com, Inc. | Email:
cmh-5nABTGoNDLqkCa/jD7nP7A@xxxxxxxxxxxxxxxx
Custom Application Development | Phone: +1-847-372-3955
http://bdistributed.com/ | Fax: +1-847-589-3738
http://bdistributed.com/Articles/ | Personal Email:
cmh-ee4meeAH724@xxxxxxxxxxxxxxxx


Thread at a glance:

Previous Message by Date:

Re: Mac OS X Help, but w/o an app

I actually just modified the plist in library/caches/com.apple.ui to have another entry like all the other entries. It's working, I'm a little concerned this isn't a very official solution though, it seems though that it works and is stable. It works very well I'd say :) -Josh On Monday, May 12, 2003, at 09:25 AM, Jérôme Laurens wrote: Make a clone of a working .help bundle (for example /Library/Documentation/Help/AirPort.help) and put it in one of the .../Library/Documentation/Help/ folders. Don't forget to assign an identifier in the plist file describing the bundle contents. You might have to log out first before the system recognizes your help book. You also have to run Apple Indexing tool if you want a searchable help. This is what I did to wrap some already existing html help. _______________________________________________ MacOSX-dev mailing list MacOSX-dev-sc6jspJ1wlD3oGB3hsPCZA@xxxxxxxxxxxxxxxx http://www.omnigroup.com/mailman/listinfo/macosx-dev

Next Message by Date:

Toolbar problems with Apple defined buttons like print

Hi I am implementing a Toolbar in my non-Document based app. I have read the Apple toolbar programming notes as well as looked at the SimpleToolbar sample app. I can get my own NSToolbarItems up and working but cannot get the Apple defined ones like the NSToolbarPrintItemIdentifier to show up. I add them to the various lists like allowed and defaults item lists. No go. Looking at the SimpleToolbar sample code from Apple, that seems to be all they do (plus some validation to make them not grayed out). If I explicitly create an NSToolbarItem with the apple defined identifier, it shows up, but without icon. Anyone have any other sample code that is not the same as the Apple sample that I can look at? (Or any other hints or gotchas?) I have been reviewing samples, reading docs, googling since Friday and have not gone forward. Thanks Chad

Previous Message by Thread:

Re: Cocoa, Java and threads

How can I realize a multithreaded AppKit application in Java? What's a useful substitue for DO in Java? Is there also no Java equivalent to: -(void) performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait Doesn't look like there is. I searched the whole com.apple.cocoa api for *thread* and *selector*, the only thing I could find was: com.apple.cocoa.foundation.NSRunLoop public void performSelectorWithOrder(NSSelector aSelector, Object target, Object anArgument, int order, NSArray modes) which looks like a native method for: @interface NSRunLoop (NSOrderedPerform) - (void)performSelector:(SEL)aSelector target:(id)target argument:(id)arg order:(unsigned)order modes:(NSArray *)modes; So I thought I could get the gui thread's runLoop, and then use this performSelectorWithOrder() method on it from my worker thread, hoping the runLoop will do the invocation from the gui thread. I can't get it to work, though. protected NSRunLoop guiRunLoop; public void awakeFromNib() { guiRunLoop = NSRunLoop.currentRunLoop(); } public void myTestMethod(Object anObject) { System.out.println("myTestMethod thread: " + Thread.currentThread()); } public void testIntraThread() { // called from worker thread NSSelector selector = new NSSelector("myTestMethod", new Class[] {Object.class}); try {selector.invoke(this, "test1");} catch(Throwable e) {} // this one works, so the selector is valid guiRunLoop.performSelectorWithOrder(selector, this, "test2", 1, new NSArray(NSRunLoop.DefaultRunLoopMode)); // this one never seems to call } Even if this would work, I would still be a bit nervous about using an NSRunLoop from a different thread because I don't know if the native implementation is thread safe or supposed to be usable from a different thread. Christian

Next Message by Thread:

How to bridge ObjC to Java? (Was: Re: Cocoa, Java and threads)

Hmm, another strategy you could probably use: Create a simple Objective-C class that just turns around and does the -performSelector... for you. [...] Then bridge that class to Java, and use it from your Cocoa-Java code. It looks a little hackish, but not all that terrible. Thanks! That looks like the best suggestion so far. Has anyone done this recently, making an ObjC class available to Java? The location of the docs in /Developer/Documentation/Cocoa/Legacy/JavaBridge and their outdated content indicate that using bridget might not be the suggested or a supported method anymore. Not knowing any alternatives, I wrote a .jobs file for my ObjC class MyClass and used it with /usr/bin/bridget. bridget created a MyClass-init.m, MyClass.java and my_package_MyClass.stubs.m file. After putting that all into a project, I can't get it to build because the linker can't find any of these symbols _NSSetJavaClassAndConstructorsForObjcClass _NSJavaLookupHandle __NSJavaStubEnter __NSJavaStubExit __NSRaiseExceptionInJava ___NSRealObjcClassForId I did an fgrep on everything in /System/Library/ but couldn't find anything containing these symbols. Does anyone know if /usr/bin/bridget and all the headers in /Developer/Java/Headers/ are still working, and if yes, how? Christian
blog comments powered by Disqus

Home | News | Sitemap | FAQ | advertise | OSDir is an Inevitable website. GBiz is too!