Re: NSThread question - DO to make it sing
Re: NSThread question - DO to make it sing
- Subject: Re: NSThread question - DO to make it sing
- From: Candide Kemmler <email@hidden>
- Date: Wed, 11 Jul 2001 19:39:39 +0200
Le mercredi 11 juillet 2001, ` 06:57, Dennis C. De Mars a icrit :
on 7/11/01 8:47 AM, Candide Kemmler at email@hidden wrote:
Le mardi 10 juillet 2001, ` 06:54, Miguel Morales a icrit :
However, this would spawn a thread, but the main thread would wait for
the secondary thread to finish before continuing, and not give you any
of the functionality you want of the main thread plugging along. This
is where DO comes in. I will assume that there is a main object that
is running in a run loop, and that when the button gets pushed the
method -(void)button:(NSData *)data is called, and that there is
another method in the object that draws to the screen (also in the
main
object) called -(oneway void)display:(bycopy NSData *)data.
Great ! I've read the chapter on DO in "Object-Oriented Programming and
the Objective-C Language" and I'm enthusiast about learning more on DO
in OC. For a java-fan like me, it's like doing RMI and multi-threading
at once ! However I find it a little odd to be forced to do rmi (or
rpc,
or whatever-you-name-it) just to do multi-threading.
You said that the code snippet you give "would spawn a thread, but the
main thread would wait for the secondary thread to finish before
continuing" !!!!! That's the whole point of doing threads !
Consider the following bit of java code:
public void actionPerformed ( ActionEvent evt ) {
if ( evt.getSource () == myButton ) {
( new Thread () {
public void run () {
renderMyImage ();
}
} ).start ();
}
}
Isn't there a way as simple as this do achieve the same effect in
Objective-C/Cocoa ?
If your "renderMyImage" routine used any Swing classes to do its
rendering,
you'd have a problem with this code too, because Swing is not
thread-safe
either; you are supposed to do all drawing in the main thread there too.
I think this is true of pretty much any GUI system of any complexity
ever
devised -- you simply have to defer drawing to the main thread.
- Dennis D.
consider the following renderMyImage implementation (not tested):
java.awt.Image renderMyImage () {
java.awt.image.BufferedImage image = new
java.awt.image.BufferedImage ( 450, 345, BufferedImage.TYPE_INT_ARGB );
Graphics2D g2d = image.createGraphics ();
g2d.setColor ( Color.red );
g2d.fillRect ( 0, 0, 450, 345 );
return image;
}
There's no Swing code involved, and now I have an image I can send to a
Swing component to draw it with something like
g.drawImage ( image, 0, 0, this );
Simple enough, huh ? And that's exactly what I'd like to do in OC/Cocoa.
Cheers,
Candide