Delaying the exit of a command line tool

Discussion in 'Mac Programming' started by hiddenpremise, Apr 15, 2010.

  1. hiddenpremise macrumors regular

    hiddenpremise

    Joined:
    Jul 25, 2008
    Location:
    Somewhere between my imagination and reality
    #1
    I am writing a Foundation tool that does some querying of opendirectory. The ODQuery object queries asynchronously so, my program exits before it returns anything. How do I stop my program from exiting until I receive a reply from my ODQuery?

    Thanks,
    Whit

    PROBLEM RESOLVED
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    You should be a bit more specific for those of us who have not written a similar tool (Actually, i guess i have, but on a different platform with different tools).

    Are you issuing a request, sleeping 1 second, checking for a result, then quitting? Couldn't you just put the check for result in a loop? If you're using some other approach, what is it? Can you post the code? at least some of it?

    -Lee
     
  3. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #3
    Use a semaphore, and signal it in the callback from your query?
     
  4. hiddenpremise thread starter macrumors regular

    hiddenpremise

    Joined:
    Jul 25, 2008
    Location:
    Somewhere between my imagination and reality
    #4
    @catfish_man: I don't understand semaphores well enough to implement them
    [EDIT] Looked it up. Tried implementing an NSLock and unlocking in my callback from my query. Didn't work. Thread just exited after the method that calls the query was done[/EDIT]

    @lee1210: running a loop delays activity in the NSRunLoop. The ODQuery object works by scheduling itself in a run loop. I don't know how to tell my program to check the runloop and wait to exit until the scheduled task has occurred.

    I have tried spawning a separate thread to call the function that schedules my query. The thread exits after that function is complete, but before the query has finished running in [NSRunLoop currentRunLoop]
     
  5. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #5
    Schedule a timer in the runloop to keep it from exiting. Point the timer's fire method to a checker that looks for a response from the query.
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    Post your code that runs the NSRunLoop.

    If you're not running the runloop, then post your code that uses or references the runloop in the main thread.

    I have a feeling that you're not familiar with runloops. Without seeing code, there's no way to debug what it's doing now, and your descriptions are simply too vague to debug.
     
  7. hiddenpremise thread starter macrumors regular

    hiddenpremise

    Joined:
    Jul 25, 2008
    Location:
    Somewhere between my imagination and reality
    #7
    This didn't help, because the thread exits after the method that i spawned it for finishes. Whatever in the run loop just gets dropped.
    I spawn the thread like this
    Code:
    NSThread *queryThread = [[NSThread alloc] initWithTarget:checkAdmin selector:@selector(doQuery) object:nil];
    	[queryThread start];
     
  8. hiddenpremise thread starter macrumors regular

    hiddenpremise

    Joined:
    Jul 25, 2008
    Location:
    Somewhere between my imagination and reality
    #8
    My xCode Project is located here http://isis.berry.edu/whit/dirservice.zip

    Thanks everyone for your help and suggestions so far
     
  9. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #9
    Your code never runs the runloop.

    It adds and removes the ODQuery, but the runloop itself is never run.

    In a Foundation Tool, no thread automatically runs its runloop, not even the main thread. That only happens in an application, and frankly, only because NSApplication does it (in short, it's not automatic, just built into NSApplication).

    At a minimum, you need to add:
    Code:
    [[NSRunLoop currentRunLoop] run];
    
    It should happen after:
    Code:
        [myQuery scheduleInRunLoop: [NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    
    And get rid of the second thread entirely. You don't need it, and coordinating that thread, its runloop, and the main thread is 10X more complex than it needs to be. A basic runloop should work fine here.

    And you should review the fundamentals of runloops, especially in the context of a Foundation Tool. If you're writing a tool that uses any of the async functionality of Cocoa, which is a considerable portion of its utility, then you will need to be extremely familiar with runloops.

    Apple's low-level info on runloops is best described in its Threading Guide. This doesn't mean you need to create threads. You just need to treat the main thread the same way the Threading Guide describes for secondary threads.

    http://developer.apple.com/iphone/l...ding/RunLoopManagement/RunLoopManagement.html

    You should also read the NSRunLoop class reference, in particular the run method and its discussion of alternatives.

    I can't point you to Apple example code, but there are plenty of searchable resources:

    http://www.cocoabuilder.com/archive/search/1?q=runloop+foundation+tool&l=cocoa

    http://www.cocoadev.com/index.pl?RunLoop

    Google keywords: site:www.cocoadev.com run loop foundation tool


    (The above may contain technical inaccuracies due to brevity, haste, and/or my own ignorance or indifference. Take them as general <handwave> outlines, not rigorous technical specifications.)
     
  10. hiddenpremise thread starter macrumors regular

    hiddenpremise

    Joined:
    Jul 25, 2008
    Location:
    Somewhere between my imagination and reality
    #10
    Chown, you rock! Thanks much :)

    Thanks all for your suggestions, tips and time!

    -Whit
     

Share This Page