Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

nsIProcess

This interface represents an executable process.
继承于: nsISupports 最后修改于Gecko 2.0 (Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1)

Implemented by: @mozilla.org/process/util;1. To create an instance, use:

var process = Components.classes["@mozilla.org/process/util;1"]
              .createInstance(Components.interfaces.nsIProcess);

Method overview

void init(in nsIFile executable);
void initWithPid(in unsigned long pid); 已废弃 Gecko 1.9.2
void kill();
void run(in boolean blocking, [array, size_is(count)] in string args, in unsigned long count);
void runAsync([array, size_is(count)] in string args, in unsigned long count, [optional] in nsIObserver observer, [optional] in boolean holdWeak);
void runw(in boolean blocking, [array, size_is(count)] in wstring args, in unsigned long count);
void runwAsync([array, size_is(count)] in wstring args, in unsigned long count, [optional] in nsIObserver observer, [optional] in boolean holdWeak);

属性

属性名称 属性类型 属性描述
exitValue long The value returned by the process upon exit. This is only valid after the process has exited. Read only.
isRunning boolean true if the process is running, otherwise false. Only accurate if the process was run with blocking disabled. Read only.
location nsIFile

The location of the executable file on disk. Read only.

Gecko 1.9.1 note
This attribute is no longer implemented as of Gecko 1.9.1, and is removed entirely in Gecko 1.9.2.

pid unsigned long The process ID of the process. This value is only available after the process has started; in addition, some platforms may not offer this value at all. Read only.
processName string

The name of the process. Read only.

Gecko 1.9.1 note
This attribute is no longer implemented as of Gecko 1.9.1, and is removed entirely in Gecko 1.9.2.

processSignature unsigned long

The process signature. Read only.

Gecko 1.9.1 note
This attribute is no longer implemented as of Gecko 1.9.1, and is removed entirely in Gecko 1.9.2.

方法

init()

Initializes the nsIProcess with the specified executable file. Once initialized, you can start the process executing by calling run().

Note: This function does not work with application bundles on Mac OS X, see bug 307463 for details.

void init(
  in nsIFile executable
);
参数
executable
The nsIFile executable file to be represented by the nsIProcess object.

已废弃 Gecko 1.9.2 (Firefox 3.6 / Thunderbird 3.1 / Fennec 1.0)

initWithPid()

Initializes the nsIProcess to represent an existing process, given that process's ID.

Gecko 1.9.1 note
This method is no longer implemented as of Gecko 1.9.1, and is removed entirely in Gecko 1.9.2.

void initWithPid(
  in unsigned long pid
);
参数
pid
The process ID to begin to represent.

kill()

立即终止该nsIProcess对象所代表的进程,只在该进程是非阻塞方式启动的情况下才有效.

Gecko 1.9.1 note
在Gecko 1.9.1 (Firefox 3.5)之前版本中, 该方法在Windows和Mac OS X下无效.目前此bug已经修复.

void kill();
参数

无.

run()

开始执行进程.

Gecko 1.9.1 note
在Gecko 1.9.1 (Firefox 3.5)之前版本中,该方法会返回一个新执行进程的进程ID,目前已经不会返回任何值.

void run(
  in boolean blocking,
  [array, size_is(count)] in string args,
  in unsigned long count
);
参数
blocking
如果为true,则该方法会阻塞,直到所打开的进程关闭为止,如果为false,则该方法会立即返回.
args
An array of count arguments, using the native character set, to be passed to the executable on its command line.
count
参数args中的参数个数.

Requires Gecko 1.9.2 (Firefox 3.6 / Thunderbird 3.1 / Fennec 1.0)

runAsync()

Asynchronously runs the process with which the object was initialized, optionally calling an observer when the process finishes running.

void runAsync(
  [array, size_is(count)] in string args,
  in unsigned long count,
  in nsIObserver observer, 可选
  in boolean holdWeak 可选
);
Parameters
args
An array of arguments to pass into the process, using the native character set. This array must have count entries.
count
The number of arguments passed in the args array.
observer 可选
An observer that will be notified when the process exits. The observer will receive this nsIProcess instance as the subject and "process-finished" or "process-failed" as the topic. The observer will be notified on the main thread.
holdWeak 可选
If true, a weak reference is used to hold the observer.
 

Requires Gecko 2.0 (Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1)

runw()

Executes the file this object was initialized with.

void runw(
  in boolean blocking,
  [array, size_is(count)] in wstring args,
  in unsigned long count
);
Parameters
blocking
If true, this method will block until the process terminates; if false, the method returns immediately.
args
An array of count arguments, using UTF-16, to be passed to the executable on its command line.
count
The number of arguments in the args array.

Requires Gecko 2.0 (Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1)

runwAsync()

Asynchronously runs the process with which the object was initialized, optionally calling an observer when the process finishes running.

void runwAsync(
  [array, size_is(count)] in wstring args,
  in unsigned long count,
  in nsIObserver observer, 可选
  in boolean holdWeak 可选
);
Parameters
args
An array of arguments to pass into the process, using UTF-16. This array must have count entries.
count
The number of arguments passed in the args array.
observer 可选
An observer that will be notified when the process exits. The observer will receive this nsIProcess instance as the subject and "process-finished" or "process-failed" as the topic. The observer will be notified on the main thread.
holdWeak 可选
If true, a weak reference is used to hold the observer.

Example

// create an nsILocalFile for the executable
var file = Components.classes["@mozilla.org/file/local;1"]
                     .createInstance(Components.interfaces.nsILocalFile);
file.initWithPath("c:\\myapp.exe");

// create an nsIProcess
var process = Components.classes["@mozilla.org/process/util;1"]
                        .createInstance(Components.interfaces.nsIProcess);
process.init(file);

// Run the process.
// If first param is true, calling thread will be blocked until
// called process terminates.
// Second and third params are used to pass command-line arguments
// to the process.
var args = ["argument1", "argument2"];
process.run(false, args, args.length);

See also

文档标签和贡献者

 此页面的贡献者: teoli, ziyunfei
 最后编辑者: teoli,