NSTask 只执行一次

发布于 2024-09-01 14:21:14 字数 1802 浏览 4 评论 0原文

我在执行不同的 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 返回值(即一切正常)。非常感谢任何指南或陷阱。

I'm having trouble executing different NSTask's. Same launchPath, different arguments. I have a class who's instances administer own NSTask objects and depending on arguments those instances were initialized with - dependent NSTask object is being created. I have two initializers:

// 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;
}

And here 's the createTask method:

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

The actions are executed via selecting different rows in NSOutlineView (using PXSourceList as a wrapper):

- (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]
}

The problem is that only first one gets executed. The second ones does not even trigger "click" event (via target-action). I think it could be cause of launchPath I'm trying to use, 'cause simple /bin/ls works fine. The same command in terminal has 0 return value (i.e. all is fine). Any guides or gotchas are much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我一向站在原地 2024-09-08 14:21:14

我无法理解为什么...但是已经阅读了许多地方< /a> NSTask 只能运行一次......

使用 NSTask,您的程序可以将另一个程序作为子进程运行,并可以监视该程序的执行。 NSTask 创建一个单独的可执行实体;与 NSThread 不同,它不与父进程共享内存空间。
默认情况下,任务继承其父环境的几个特征:当前目录、标准输入、标准输出、标准错误以及任何环境变量的值。如果您想更改其中任何一个,例如当前目录,则必须在启动任务之前设置一个新值。任务的环境在启动后就已建立。

NSTask 只能运行一次。随后尝试运行 NSTask 会引发错误。

如果您从基于文档的应用程序中的文档运行任务,您应该(至少)在文档的清理代码中将终止消息发送到任务实例。 另请参阅 NSTaskTermination 了解更多讨论。

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

I can't comprehend why... but have read from numerous places that NSTask CAN ONLY be run once....

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.

This seems ridiculous... I will research and post back if I find any info the contradicts this source, (although it is usually reliable.)

以歌曲疗慰 2024-09-08 14:21:14

如果您愿意涉足 PyObjC 周围的浑水,您可以轻松使用 Python 的子进程机制......一遍又一遍。哦,是的。

If you care to wade the murky waters that surround PyObjC, you can easily use the subprocess mechanism of Python.. over, and over, and over. Oh yeah.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文