Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

nikiski

macrumors regular
Original poster
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. :)
 

wrldwzrd89

macrumors G5
Jun 6, 2003
12,110
77
Solon, OH
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

macrumors G4
Nov 3, 2005
10,434
12,250
UK
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

macrumors 68000
Jun 6, 2003
1,950
0
District of Columbia
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

macrumors 601
Sep 8, 2003
4,101
1,312
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

macrumors 68040
Oct 25, 2004
3,540
272
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

macrumors G4
Nov 3, 2005
10,434
12,250
UK
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

macrumors regular
Original poster
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

macrumors 6502a
Jan 4, 2002
981
0
Austin, TX
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

macrumors 601
Sep 8, 2003
4,101
1,312
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."
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.