Re: Follow-up: Cocoa thread-safety
Re: Follow-up: Cocoa thread-safety
- Subject: Re: Follow-up: Cocoa thread-safety
- From: Steve Klingsporn <email@hidden>
- Date: Sat, 26 Oct 2002 11:06:26 -0500
Hi Dan,
This is one of the only methods I have not tried yet. I'll try it out
now,
as I've had some rest and that splitting headache I had last night is
gone. =)
Else, I will have to use an NSTimer, which is a yucky "solution" -- and
I'll group together the code that appends text to my NSView's
NSTextContainer with the code the scrolls and call them together from
the main thread every second when there is incoming chat text from
my connection, and there won't be a delay (that the user can tell, as
the
user won't know that the message was processed <= 1 second later than
it was received). I'll keep calling the code from the main thread when
the user types a message and the response will be instantaneous.
Maybe if I call my NSTimer once a second, it will use only 1% of the CPU
where it uses 4% when I call it four times a second, I dunno.
(On BeOS, 4% of the CPU is what it took to play an MP3! Compare to
15-35% on "the world's most advanced operating system..." =/ )
Thanks; I will try the NSEvent approach. Maybe it will work!
Steve
On Saturday, October 26, 2002, at 12:19 AM, OS X AIBO wrote:
Hmm. I don't know what the best solution is, but this is what came to
mind.
You want the main thread to remain idle unless there is something for
the user to see or the user performs some action. Since the main thread
is good at waiting for events, why not send it an event from your
worker thread once it has received data over the net.
Check out NSApplication's public void postEvent( NSEvent anEvent,
boolean flag)
"Adds anEvent to the receiver's event queue. If flag is true, the
event is added to the front of the queue, otherwise the event is added
to the back of the queue.
This method can also be called in subthreads. Events posted in
subthreads bubble up in the main thread event queue."
Then check out NSEvent's "Other Events" topic.
"... Of the three miscellaneous event types, only NSApplicationDefined
is of real use to application programs. It allows the application to
generate totally custom events and insert them into the event queue.
Each such event can have a subtype and two additional codes to
differentiate it from others.
otherEventWithType:location:modifierFlags:timestamp:windowNumber:contex
t:subtype:data1:data2
creates one of these events, and the subtype, data1, and data2 methods
return the information specific to these events."
Let me know if that works out. I could end up using this myself.
Cheers,
--Dan
--- Steve Klingsporn <email@hidden> wrote:
The following is written in the 10.2 Cocoa release notes:
New methods for performing selectors on the main thread
Sometimes a background thread must have the main thread perform some
work on its behalf. These are often UI-related things, like updating
a
window's displayed state. Two new methods on NSObject, declared in
<Foundation/NSThread.h>, have been added in 10.2.
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg
waitUntilDone:(BOOL)wait modes:(NSArray *)array;
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg
waitUntilDone:(BOOL)wait;
Unfortunately, NSThread is not available from Java, nor do these
methods seem to be relocated as the Java counterparts often are. I
suppose it might be possible to write some code that uses JNI or the
Java Bridge to do this, but this violates my desire to keep
everything
Java. My timer approach works, but using 4% of the CPU to run a
timer
4 times a second seems a bit extreme to me.
Any help would be appreciated. Thank you.
Steve
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
Y! Web Hosting - Let the expert host your web site
http://webhosting.yahoo.com/
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.