gpt4 book ai didi

cocoa - NSTask 只执行一次

转载 作者:行者123 更新时间:2023-12-03 16:21:47 28 4
gpt4 key购买 nike

我在执行不同的 NSTask 时遇到问题。相同的launchPath,不同的参数。我有一个类,其实例管理自己的 NSTask 对象,并根据参数初始化这些实例 - 正在创建依赖的 NSTask 对象。我有两个初始化器:

// Method for finished task
- (void)taskFinished:(NSNotification *)aNotification {
[myTask release];
myTask = nil;

[self createTask];
}

// Designated initializer
- (id) init {
self = [super init];
if (self != nil) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(taskFinished:)
name:NSTaskDidTerminateNotification
object:nil];
[self createTask];
}
return self;
}

// Convenience initializer
- (id)initWithCommand:(NSString *)subCommand {
self = [self init];
if (self)
{
[self setCommand:subCommand];
}
return self;
}

这是 createTask 方法:

- (void)createTask {
// myTask is a property defined as NSTask*
myTask = [[NSTask alloc] init];
[myTask setLaunchPath:@"/usr/bin/executable"];
}

通过在 NSOutlineView 中选择不同的行来执行操作(使用 PXSourceList 作为包装器):

- (void)sourceListSelectionDidChange:(NSNotification *)notification {
id sourceList = [notification object];
NSIndexSet *selection = [sourceList selectedRowIndexes];
NSString *identifier = [[sourceList itemAtRow:[selection firstIndex]] identifier];

// this way `/usr/bin/executable ${identifier}` is being created
MyCommand *command = [[MyCommand alloc] initWithSubcommand:identifier];

// this method executes [myTask launch];
[command execute]
}

问题是只有第一个被执行。第二个甚至不会触发“点击”事件(通过目标操作)。我认为这可能是我尝试使用 launchPath 的原因,因为简单的 /bin/ls 工作正常。终端中的相同命令有 0 返回值(即一切都很好)。非常感谢任何指南或陷阱。

最佳答案

我无法理解为什么...但已阅读numerous places NSTask 只能运行一次......

Using NSTask, your program can run another program as a subprocess and can monitor that program’s execution. NSTask creates a separate executable entity; unlike NSThread, it does not share memory space with the parent process. By default, a task inherits several characteristics of its parent's environment: the current directory, standard input, standard output, standard error, and the values of any environment variables. If you want to change any of these, e.g., the current directory, you must set a new value before you launch the task. A task’s environment is established once it has launched.

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

If you run a task from a document in a document-based application, you should (at the very least) send the terminate message to the task instance in the cleanup code for the document. See also NSTaskTermination for more discussion.

这看起来很荒谬......如果我发现任何信息与此来源相矛盾,我会研究并发布回来,(尽管它通常是可靠的。)

关于cocoa - NSTask 只执行一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2846747/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com