PDA

View Full Version : Multi-threaded Programing with Cocoa




nikiski
Mar 12, 2008, 08:22 AM
Hi all, I am currently exploring different way to write multithreaded programs. Say I have a NSButton and a NSTextField, I also have a random string generator.

What I want to accomplish is that the string generator runs every x amount of seconds and updates the textfield, and when the button is pressed a random string is generated immediately while delaying the automatic run of the string generator by x amount of seconds. Say the string generator runs for 1 second and it isn't quite practical to have the whole UI freeze when it is running.

Any good suggestions? Thanks in advance. :)



Eraserhead
Mar 12, 2008, 08:47 AM
Look at NSThread.

wrldwzrd89
Mar 12, 2008, 09:09 AM
You could also use NSOperation, but only if you're willing to sacrifice compatibility with pre-Leopard Mac OS X users. My understanding is that NSOperation is only available in Leopard.

Eraserhead
Mar 12, 2008, 09:17 AM
You could also use NSOperation, but only if you're willing to sacrifice compatibility with pre-Leopard Mac OS X users. My understanding is that NSOperation is only available in Leopard.

NSThread has also seen large improvements in Leopard, so its worth baring in mind.

savar
Mar 12, 2008, 10:01 AM
Hi all, I am currently exploring different way to write multithreaded programs. Say I have a NSButton and a NSTextField, I also have a random string generator.

What I want to accomplish is that the string generator runs every x amount of seconds and updates the textfield, and when the button is pressed a random string is generated immediately while delaying the automatic run of the string generator by x amount of seconds. Say the string generator runs for 1 second and it isn't quite practical to have the whole UI freeze when it is running.

Any good suggestions? Thanks in advance. :)

You don't need threads to accomplish this (although it could work) -- you could accomplish the same thing through NSTimer. The timer approach is easier because you don't have to communicate the button status between threads.

Krevnik
Mar 12, 2008, 01:04 PM
You don't need threads to accomplish this (although it could work) -- you could accomplish the same thing through NSTimer. The timer approach is easier because you don't have to communicate the button status between threads.

Approved. :)

Seriously, for something like this, threading is nice, but adds complexity. Plus, you have the bonus that NSTimer just about has all the functionality you need, and doesn't block your thread either.

iSee
Mar 12, 2008, 01:39 PM
The OP did mention that the "string generator" itself would take a significant amount of time and didn't want the UI to pause while this was happening. To meet that requirement, I don't think NSTimer by itself will be sufficient.

You're going to need to at least run the string generator on different thread than the UI.

Eraserhead
Mar 12, 2008, 01:45 PM
I have something in a different thread, so it can be cancelled and I use a Boolean to communicate between them, I'm not sure whether that is totally sound, but as its just a Boolean I think it should be OK...

nikiski
Apr 7, 2008, 08:53 AM
Thank you all very much for your input. I have resorted to use NSOperation and NSNotificationCenter as they pretty much do what I need to.

But I have another question, it's regarding IB and it's a little off topic, but I don't want to create a thread just for it.

It's about drawers. When I resize the parent window the drawer will get detached for some reason and it floats by itself. Only when I click on toggle will it resize properly. I have the drawer attached to the bottom of the parent window. I know I should have to code for it, but it's a bit annoying and ugly. Does anyone know where I can do that in IB? Or do I really have to code something?

(Note, I have seen the DrawerMadness example but I can't pick up any differences between it and my project.)

Sayer
Apr 7, 2008, 09:44 AM
It's about drawers. When I resize the parent window the drawer will get detached for some reason and it floats by itself. Only when I click on toggle will it resize properly. I have the drawer attached to the bottom of the parent window. I know I should have to code for it, but it's a bit annoying and ugly. Does anyone know where I can do that in IB? Or do I really have to code something?

(Note, I have seen the DrawerMadness example but I can't pick up any differences between it and my project.)

Drawers should die instead of 64-bit carbon IMHO. If you really need some piece of UI to show up only in response to user action try using a sheet instead. Yeah a sheet is modal, but really if the UI needs to almost always be available then make a multi-pane view, or master-detail view.

Drawers are, just, yuck, so last century. Hey people can dog on other once-whizzy parts of OS X as being outmoded so can I.

Krevnik
Apr 7, 2008, 11:53 AM
Drawers should die instead of 64-bit carbon IMHO. If you really need some piece of UI to show up only in response to user action try using a sheet instead. Yeah a sheet is modal, but really if the UI needs to almost always be available then make a multi-pane view, or master-detail view.

Another +1. :)

Drawers were a sort of weird UI control that really never worked as well as one hoped. Apple for the most part has abandoned drawers in their own software such as Mail, iCal and others.

You can do modal sheets, maybe a utility pane, or even a custom control for what you need... but in general, I haven't seen a usage of drawers that make users go "Oh hey, that works."

nikiski
Apr 7, 2008, 07:23 PM
Mmm. Then utility pane it is. :D

Thanks guys! Much appreciated. :)