Re: silly event question 'cuz i'm a n00b
Re: silly event question 'cuz i'm a n00b
- Subject: Re: silly event question 'cuz i'm a n00b
- From: Andrew Merenbach <email@hidden>
- Date: Wed, 8 Oct 2008 20:11:42 -0700
On Oct 8, 2008, at 7:46 PM, John Zorko wrote:
Hello, all ...
If a certain flag is set, my app needs to advance to the next item
in a list to process it when the previous item is done. However, I
want the user to still be able to stop it, so I don't want to hang
the UI. In effect, if this flag is set, I want the app to act as if
the user had manually selected the next item to process.
In Win32, one way I could do this is by using PostMessage() -- when
the current item is finished processing, it would post a message to
the relevant threads' message queue and it would get processed, just
as if the message was put there as a result of a GUI action. How do
I do this in Cocoa, or is there a better way?
Regards,
John
Greetings, John,
If you can afford to target Leopard-only, you might look into
NSOperation and NSOperationQueue. They provide a way to manage
sequential (or even concurrent) thread operations in a relatively
simple fashion. There's a great tutorial at <http://www.macresearch.org/node/4567
> about multithreading using those classes. Despite that the
particular article's title starts out with "Cocoa for Scientists," you
needn't worry if you're not in the field of science: you can easily
adapt their code for your own ends.
Apple's documentation on "Managing Concurrency with NSOperation" is
also a good read, and you can find it at <http://developer.apple.com/cocoa/managingconcurrency.html
>.
Granted, it sounds as though you *might* (just might) be porting a
Windows app, in which case using NSOperation might not be ideal, since
it might very easily require some major refactoring. If you have the
option of using it, however, it will very likely make things simpler
for you -- one item after another will be executed from the queue, and
you can make each item dependent upon its predecessor, if you like, in
order to share or accumulate data (I believe that the Apple link that
I gave you has an example that does this).
Another option would be NSNotification and NSNotificationCenter (and
possible NSNotificationQueue, although I've never used that particular
class myself). This is more along the lines of what you are
describing, except for this (from the NSNotificationCenter
documentation): "In a multithreaded application, notifications are
always delivered in the thread in which the notification was posted,
which may not be the same thread in which an observer registered
itself."
Thus you won't be able to communicate *between* threads using
NSNotificationCenter.
One method, which may be a lead for you, is
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:
(id)argwaitUntilDone:(BOOL)wait
This can be used send a message to the main thread, to signal that it
needs to start processing another item.
Hopefully at least some of this will prove relevant!
Cheers,
Andrew
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
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