PDA

View Full Version : NSTask Not terminating.




DavidBlack
Aug 2, 2013, 01:23 PM
Ok, I have this code for this program that creates symbolic links. When the create symbolic link button is pressed this code is executed:

[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:

[symLinkTask terminate];


But when I press the button a second time I get this error:
Application Specific Information:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'task already launched'

Which is weird because the NSTask should not be running.



chown33
Aug 3, 2013, 12:27 AM
There is a C function that creates symlinks. Read its man page.
https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man2/symlink.2.html

Persifleur
Aug 3, 2013, 07:15 AM
https://developer.apple.com/library/mac/#documentation/cocoa/Reference/Foundation/Classes/NSTask_Class/Reference/Reference.html

An NSTask object can only be run once. Subsequent attempts to run the task raise an error.

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.

DavidBlack
Aug 3, 2013, 12:26 PM
There is a C function that creates symlinks. Read its man page.
https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man2/symlink.2.html

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: NSString *myNewString = [path1 absoluteString];
? and then to. Const char?

----------

https://developer.apple.com/library/mac/#documentation/cocoa/Reference/Foundation/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.

Thanks for your help :)

DavidBlack
Aug 3, 2013, 04:56 PM
There is a C function that creates symlinks. Read its man page.
https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man2/symlink.2.html

Thanks for your help chown33 but I think I will create my symlink with the NSFileManager instead. :)

chown33
Aug 3, 2013, 09:07 PM
Thanks for your help chown33 but I think I will create my symlink with the NSFileManager instead. :)

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.