NSTask只执行一次 [英] NSTask executed only once

查看:209
本文介绍了NSTask只执行一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这里是 createTask 方法:

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

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

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

问题是,执行。第二个甚至不触发点击事件(通过目标行动)。我认为这可能是launchPath我试图使用的原因,因为简单 / bin / ls 工作正常。终端中的相同命令具有0返回值(即,所有都是精确的)。

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.

推荐答案

我不能理解为什么 ...已从阅读了许多地方 NSTask只能运行一次....

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


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

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.

NSTask只能运行一次。

如果您从基于文档的应用程序中的文档运行任务,您应该最少)将终止消息发送到文档的清除代码中的任务实例。

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.

这似乎很可笑...我会研究并回传if我发现任何信息与这个来源矛盾,(虽然它通常是可靠的。)

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

这篇关于NSTask只执行一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆