如何在 adobe air 桌面应用程序中重新启动 NativeProcess?

How to restart a NativeProcess in adobe air desktop application?

我正在 运行从我们的 adobe air 应用程序中下载一个 .net .exe 文件。 .exe 文件接收不同的参数。因此,当我必须发送新参数时,我会停止该过程并使用新参数重新开始。我正在使用 NativeProcess 的 exit() 方法,但是当我再次启动 exe 时,它​​会给我

Error: Error #3213: Cannot perform operation on a NativeProcess that is already running. at Error$/throwError() at flash.desktop::NativeProcess/start()

我已将 nativeProcess 初始化为静态变量:

public  static var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
public static var process:NativeProcess = new NativeProcess();

然后在单击事件中我调用了 exe 文件

var file:File = File.desktopDirectory;
file = file.resolvePath("C:\somepath.exe");
var ar:Vector.<String> = new Vector.<String>();
ar.push("0");
ar.push("C:\temp\APIData.xml");

nativeProcessStartupInfo.executable = file;
nativeProcessStartupInfo.arguments = ar;
process.start(nativeProcessStartupInfo);

这个 运行 代码绝对没问题。在另一个点击事件中,我调用

process.exit()

然后再次 运行 exe

var file:File = File.desktopDirectory;
file = file.resolvePath("C:\somepath.exe");
var ar:Vector.<String> = new Vector.<String>();
ar.push("1");
ar.push("C:\temp\APIData.xml");

nativeProcessStartupInfo.executable = file;
nativeProcessStartupInfo.arguments = ar;
process.start(nativeProcessStartupInfo);

但是在另一个点击事件中,我已经 运行ning 收到了 NativeProcess 的错误。有人可以帮助我做错什么吗?

如果您在退出后立即重新启动进程,则退出可能需要更长的时间,并且进程尚未 "exited"。

尝试监听进程真正结束时调度的退出事件,然后重新启动进程:

process.addEventListener(NativeProcessExitEvent.EXIT, onProcessExit);

private function onProcessExit(e:NativeProcessExitEvent):void
{
    // restart your process from here
}

当您调用 exit() 时,不能保证进程会结束,因为它发生在 flash 范围之外。作为最后的手段,您可以尝试强制进程退出:

process.exit(true);