Multi-threaded Programing with Cocoa

Discussion in 'Mac Programming' started by nikiski, Mar 12, 2008.

  1. macrumors regular

    Joined:
    Oct 30, 2007
    Location:
    Hong Kong, or Surrey, BC, Canada
    #1
    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. :)
     
  2. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
  3. macrumors G4

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #3
    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.
     
  4. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #4
    NSThread has also seen large improvements in Leopard, so its worth baring in mind.
     
  5. macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Location:
    District of Columbia
    #5
    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.
     
  6. macrumors 68020

    Krevnik

    Joined:
    Sep 8, 2003
    #6
    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.
     
  7. macrumors 68040

    iSee

    Joined:
    Oct 25, 2004
    #7
    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.
     
  8. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #8
    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...
     
  9. thread starter macrumors regular

    Joined:
    Oct 30, 2007
    Location:
    Hong Kong, or Surrey, BC, Canada
    #9
    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.)
     
  10. macrumors 6502a

    Sayer

    Joined:
    Jan 4, 2002
    Location:
    Austin, TX
    #10
    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.
     
  11. macrumors 68020

    Krevnik

    Joined:
    Sep 8, 2003
    #11
    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."
     
  12. thread starter macrumors regular

    Joined:
    Oct 30, 2007
    Location:
    Hong Kong, or Surrey, BC, Canada
    #12
    Mmm. Then utility pane it is. :D

    Thanks guys! Much appreciated. :)
     

Share This Page