停止程序直到 JSFL 文件完成
我正在尝试从 C# 项目中运行 JSFL 脚本,其中 JSFL 脚本打开一个 .fla 文件,对其进行 n 次修改,并在关闭之前导出 n 个 .swf 文件。在 C# 项目中,我通过 Process 类启动 JSFL 脚本。我尝试通过 myProcess.WaitForExit() 命令等待 JSFL 进程完成,但它不起作用。我的 C# 代码的其余部分在 JSFL 进程完成导出其 .swf 文件之前执行。有什么方法可以抛出 C# 项目可以从 JSFL 文件或其他解决方案中捕获的内容吗?
I am trying to run a JSFL script from within a C# project, where the JSFL script opens up a .fla file, modifies it n times, and exports n .swf files before closing. From the C# project, I am starting up the JSFL script through the Process class. I try to wait for the JSFL process to finish through the myProcess.WaitForExit() command, but it doesn't work. The remainder of my code C# executes before the JSFL process finishes exporting its .swf files. Is there any way I can throw something that the C# project can catch from the JSFL file or some other solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
一种解决方案(尽管很可能不是最好的解决方案)是让您的 C# 代码查找正在创建的 SWF 文件。只有更新/创建它们后,JSFL 脚本才会完成,因此您知道您的 C# 可以安全地继续。如果无法知道 C# 程序需要查找多少个 SWF 文件,您可以让 JSFL 脚本写入某种日志文件,并让 C# 程序每隔一段时间读取该日志,查找某种标记来指示 JSFL 脚本已完成其任务。
诚然,这可能远不是最好的方法(而且我对 C# 的了解还不够深,无法知道您所看到的内容是设计使然还是出现问题的迹象)。但运行 JSFL/Flash 可能是完全异步的,如果是这样,也许日志文件方法是解决问题的最快路径。
One solution (although most likely not the best one) would be for your C# code to look for the SWF files being created. Only once they've been updated/created will the JSFL script have finished, so you know that it will be safe for your C# to move on. In case there is no way of knowing how many SWF files the C# program needs to look for, you could instead let the JSFL script write to a log file of some sort, and to have the C# program read that log on a interval, looking for some sort of token to indicate that the JSFL script has completed it's task.
Granted, this may be far from the best method (and I don't know C# well enough to know whether what you're seeing is by design or a sign of something being wrong). But it may just be the case that running JSFL/Flash is completely asynchronous and if so, maybe the log file approach is the quickest path to solving the problem.
我对另一个调用外部 JSFL 脚本的应用程序也有同样的问题。
我所做的是将文件完成后写入目标目录,然后在调用应用程序中轮询该文件。文件一出现,我就知道任务已经完成,然后我删除临时文件,然后继续。哦 - 在开始之前,我会查找并删除所有临时文件,这样我就不会得到误报。
这并不像事件那么美好,但你必须利用你所拥有的一切。
I have the same problem with another application that calls an external JSFL script.
What I do is write a file once finished to a target directory, and in the calling application, poll for that file. As soon as the file appears, I know the task has finished, and I delete the temp file, and carry on. Oh - and before starting I look for and delete any temp files so I don't get a false positive.
It's not so nice as events, but you have to work with what you've got.
您是否尝试过设置一个自定义侦听器,该侦听器将在 JSFL 完成时执行一个函数。不要忘记它仍然基于 ECMA,这是一种过程语言。
顺便说一句,JSFL 有一个低级 C API。
C 级 API
Have you tried to set a custom Listener that will execute a function when the JSFL done. Don't forget that it's still based on ECMA which is a procedual language.
By the way, JSFL has a LOW-LEVEL C API.
C LEVEL API
这绝对是可能的,我已经在堆栈溢出上发布了一个解决方案,其中包含详细的问题描述以及实现它所需的所有 C# 和 JSFL 源代码:自动发布 FLA 文件;多次调用 Process.Start
总结一下...首先,等待 JSFL 脚本进程是没有用的,因为您实际上是在调用 Flash.exe,Flash.exe 在 JSFL 完成后将保持打开/运行状态,所以您将等待一个永远不会发生的进程退出事件。
诀窍是在 JSFL 脚本末尾使用 JSFL 函数,该函数执行 Windows 命令行命令,并且该调用将向您的 C# 应用程序发出 JSLF 脚本已完成的信号。因此,基本上您将拥有 C# 应用程序的主实例,但您想要修改 C# 应用程序,以便可以使用特定开关从命令行运行第二个实例。当您使用特定开关(从 JSLF 文件)调用 C# 应用程序时,您希望程序不正常运行,而只是在应用程序的主实例中发出等待句柄信号,然后关闭。我使用一个名为 XDMessaging 的简单第三方开源库,它可以促进此类进程间通信。它允许您在 C# 应用程序中打开一个命名通道,该通道可以接收来自第二个实例的命令,向主实例发出信号,表明 JSFL 脚本已完成并且可以安全地继续。
This is absolutely possible, and I've already posted a solution here on stack overflow, complete with a detailed problem description and all the C# and JSFL source code necessary to implement it: Automating publishing of FLA files; calling Process.Start multiple times
To summarize... first of all, waiting on the JSFL script process is useless, because you're actually calling Flash.exe, which will remain open/running after the JSFL completes, so you'd be waiting on a process exit event that will never occur.
The trick is to use a JSFL function at the end of the JSFL script which executes a windows command line command, and it's that call which will signal your C# app that the JSLF script has completed. So basically you'll have your main instance of your C# app, but you want to modify your C# app so that a 2nd instance can be run from a command line with a particular switch. When you're C# app is called with a particular switch (from the JSLF file), then instead of running normally, you want your program to simply signal a wait handle in the main instance of your application and then close. I use a simple third-party open-source library called XDMessaging, which facilitates such inter-process communication. It lets you open a named channel in your C# app, which can receive a command from the 2nd instance, signaling the main instance that the JSFL script has finished and it's safe to continue.
但是像这样涉及文件观察器并不是最好的解决方案,所以我捕获 Flash 进程并监视进程标题,并告诉 JSFL 在执行完成后为 Flash 窗口渲染一些窗口标题。
But involving file watcher like this is not the best solution so I catch the Flash process and keep watch on the process title and tell the JSFL render some window title for the flash window after finish the execution.