NSTask Not terminating.

Discussion in 'Mac Programming' started by DavidBlack, Aug 2, 2013.

  1. macrumors 6502a

    DavidBlack

    Joined:
    Jan 27, 2013
    Location:
    Somewhere In Apple's HQ ;)
    #1
    Ok, I have this code for this program that creates symbolic links. When the create symbolic link button is pressed this code is executed:

    Code:
       [symLinkTask setArguments:[NSArray arrayWithObjects:@"-s", sourcePath, targetPath, nil]];
            
            [symLinkTask launch];
            [symLinkTask waitUntilExit];
    Then an alert pops up and when the OK button is return and I terminate the NSTask.

    Using this:

    Code:
            [symLinkTask terminate];
    
    But when I press the button a second time I get this error:
    Code:
    Application Specific Information:
    *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'task already launched'
    Which is weird because the NSTask should not be running.
     
  2. macrumors 603

    Joined:
    Aug 9, 2009
    #2
  3. macrumors member

    Joined:
    Jun 1, 2005
    Location:
    London, UK
    #3
    https://developer.apple.com/library...Classes/NSTask_Class/Reference/Reference.html

    The error isn't that the task is still running. It's that the task has already run and completed, and can't be started again. You'll need to create a new NSTask object to run the task again. But chown's suggestion of using the C function is a better solution.
     
  4. DavidBlack, Aug 3, 2013
    Last edited: Aug 3, 2013

    thread starter macrumors 6502a

    DavidBlack

    Joined:
    Jan 27, 2013
    Location:
    Somewhere In Apple's HQ ;)
    #4
    Thanks chown33 I will use this. It's worth noting that I am using a NSURL to get the path of the original file so I will have to convert the NSURL to a const char?
    Or convert the NSURL to a NSString using:
    Code:
    NSString *myNewString = [path1 absoluteString];
    ? and then to. Const char?

    ----------

    Thanks for your help :)
     
  5. thread starter macrumors 6502a

    DavidBlack

    Joined:
    Jan 27, 2013
    Location:
    Somewhere In Apple's HQ ;)
    #5
  6. macrumors 603

    Joined:
    Aug 9, 2009
    #6
    That also works.

    By prompting you to look for a simpler solution (a C function), you found a different and also simpler solution. A lot of programming is about finding a simpler solution, rather than trying to make a complex one work.
     

Share This Page