Open a second window and pause the code?

Discussion in 'Mac Programming' started by larswik, Aug 22, 2011.

  1. macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    I have a button that triggers a method. When the method is executed I have a second window pop up that asks for the level of the creature and if he was killed or still alive to do some math. When the second window opens I want the code in the method to pause or wait until the new information is entered so it can do the math correctly adding bonuses and what not.

    I thought this might work but I get a beach ball of death.
    Code:
    - (IBAction)endCombatButton:(id)sender {
        doaWindowOpen = YES;
        [doaWindow makeKeyAndOrderFront:self];
        while (doaWindowOpen) {
            ;
        }
    
    With the close button on the second window making the doaWindowOpen = NO; which should exit the loop and continue the code.
    Code:
    - (IBAction)doaWindowCloseButton:(id)sender {
        doaWindowOpen = NO;
        [doaWindow orderOut:self];
    }
    Is there some code I am unaware of that will hold the rest of the method from executing until I close the second window?
     
  2. thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #2
    I thought of a solution that should work. I took the rest of the code that was supposed to execute and pretty much copy and pasted it the the 'close button' method for the second window. Then on the original button I just left the open the second window code. When the second window closes the rest of the code gets executed.

    If there is a to pause the code I would love to know since I am learning.

    Thanks
     
  3. macrumors 6502a

    Joined:
    Jun 27, 2010
    #3
    I don't know about the mac or cocoa specific solution. But you should use some kind of thread synchronization object. This is a much better solution than polling a bool.

    In windows, there are Events you can create and wait on. This will "pause" code running on one thread until you release it from another thread(the UI thread).

    Running through the docs really quickly reveals NSConditionLock. This might be what you need.
     
  4. foidulus, Aug 22, 2011
    Last edited: Aug 22, 2011

    macrumors 6502a

    Joined:
    Jan 15, 2007
    #4
  5. thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #5
    Thanks! I will take a look at the link.

    Obj -C is so vast. I spent the last 45 minutes now looking for the information window that displays it's self when you hover over a button or object that explains that tool.

    Thanks again!
     
  6. macrumors 6502a

    Joined:
    Jan 15, 2007
    #6
    Well, to be pedantic its not really Objective-C that has all the windowing stuff, it is Cocoa which is built on top of Objective-C. Objective-C is a programming language, Cocoa a framework.
     
  7. thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #7
    Ya yes, that is true. I am starting my Java class at City College this Wednesday. The one thing about the last Pascal class I took was the interaction with other programmers and getting the lingo and proper nomenclature, which is hard to develop learning by myself at home.

    Thanks.
     
  8. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #8
    Good luck with the Java class. I'm tutoring a friend thought a masters class in Java at the moment. Java from 0 to 60 mph in 10 weeks.

    Nomenclature is definitely a challenge, especially when we as computer scientists overload terms, like object. But it is vary important to get it right. You do need to be able to communicate effectively with your future colleagues, and the lingo goes a long a way to facilitating this.
     
  9. macrumors 6502

    Joined:
    Feb 7, 2008
    #9
    Your soution is a good one, and a Modal window would work too. I thought I would give a quick explaination of why you got a beach ball though, as it is part of how Cocoa works.

    Cocoa is event based. Everything happens in relationship to an event. For example, when you click a button, and an event happens. There are other events too.

    The catch is that only one line of code can execute at a time. (unless your program is multithreaded, which is a more advanced topic)

    In the operating system, there is an event loop, that waits for an event, then when one occurs it calls the appropriate code. When that code is done executing, control returns to the event loop, waiting for the next event.

    Usually this is invisible, a method will execute and return control to the event loop so fast you never notice it. But if you create a while loop like you did, control never returns to the event loop. So events can not be detected in you new window, and it is imposible for the BOOL condition to change. So it truly turns into an infinate loop, and OS/X throughs up a beach ball.
     
  10. thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #10
    Thanks for the explanation. I have another part in the code where I need a window called. I think I will call the Modal window approach for this one.

    I have to say, the more I learn about programming the more fun it is becoming. Last night I sat down at 6p and stopped at 3am. I even skipped having dinner last night and just kept working.
     
  11. macrumors 6502a

    Joined:
    Dec 2, 2008
    #11
    You are not in control

    In Cocoa, you are not in control. The run loop is in control. It does what it wants when it wants. Your code is not in control. Your code runs within the (invisible) run loop. The run loop calls methods in your code (and other methods that you don't see).

    To answer your specific question, foidulus is correct: you want a model window.

    On a larger scale, wlh99 is right. My first paragraph above reinforces this.

    Do not fight Cocoa. You will lose. Accept your place in the universe (below Cocoa, above single-celled organisms).
     

Share This Page