AS3 Worker在Flash CC调试或发布时无法正常运行,发布时除外

时间:2015-07-22 09:15:15

标签: actionscript-3 flash air flashdevelop flash-cc

今天我尝试使用AS3 Worker类进行一些后台工作。

但是,在Windows 7上的Flash CC中开发我的AIR桌面应用程序时,我遇到了奇怪的行为。请考虑这个主要“线程”的简单文件:

////////////////
// MainThread.as
////////////////
package{
    import flash.display.*;
    import flash.system.*;
    import flash.events.*;
    import flash.utils.*;
    public class MainThread extends MovieClip
    {
        [Embed(source="WorkerThread.swf", mimeType="application/octet-stream")]
        private static var WORKER_SWF:Class;

        var mainToWorker:MessageChannel;
        var workerToMain:MessageChannel;
        var workerToMainStartup:MessageChannel;

        public function MainThread()
        {
            var workerBytes:ByteArray = new WORKER_SWF() as ByteArray;
            var worker:Worker = WorkerDomain.current.createWorker(workerBytes, true);

            // Send to worker
            mainToWorker = Worker.current.createMessageChannel(worker);
            worker.setSharedProperty("mainToWorker", mainToWorker);

            // Receive from worker
            workerToMain = worker.createMessageChannel(Worker.current);
            workerToMain.addEventListener(Event.CHANNEL_MESSAGE, onWorkerToMain);
            worker.setSharedProperty("workerToMain", workerToMain);

            // Receive startup message from worker
            workerToMainStartup = worker.createMessageChannel(Worker.current);
            workerToMainStartup.addEventListener(Event.CHANNEL_MESSAGE, onWorkerToMainStartup);
            worker.setSharedProperty("workerToMainStartup", workerToMainStartup);

            worker.start();

            trace("hi");
        }

        private function onWorkerToMain(ev:Event): void
        {
        }

        private function onWorkerToMainStartup(ev:Event): void
        {
            var success:Boolean = workerToMainStartup.receive() as Boolean;
            trace(success);
            if (!success)
            {
                // ... handle worker startup failure case
            }
        }
    }
}

然后这个工人的文件(这个文件的swf是用FlashDevelop构建的):

//////////////////
// WorkerThread.as
//////////////////
package{
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.system.*;
    import flash.events.*;
    import flash.filesystem.*;

    public class WorkerThread extends MovieClip
    {
        public var mainToWorker:MessageChannel;
        public var workerToMain:MessageChannel;
        public var workerToMainStartup:MessageChannel;

        public function WorkerThread()
        {
            // Receive from main
            mainToWorker = Worker.current.getSharedProperty("mainToWorker");
            mainToWorker.addEventListener(Event.CHANNEL_MESSAGE, onMainToWorker);

            // Send to main
            workerToMain = Worker.current.getSharedProperty("workerToMain");

            // Send startup message to main
            workerToMainStartup = Worker.current.getSharedProperty("workerToMainStartup");
            workerToMainStartup.send(true);

            trace("Hello from worker world.");
            var file:File = File.desktopDirectory.resolvePath("MyTextFile.txt");
            var stream:FileStream = new FileStream();
            stream.open(file, FileMode.WRITE);
            stream.writeUTFBytes("ZA WARUDO.");
            stream.close();
        }

        private function onMainToWorker(event:Event): void
        {
        }
    }
}

以下是我在从Flash CC构建和启动应用程序时遇到的问题

  • 在Flash CC调试器中无法看到来自工作者世界的痕迹。
  • onWorkerToMainStartup监听器永远不会被触发,尽管工作人员正在运行(在短暂的延迟后跟踪worker.state将确认)。
  • 工作者应该写一个文件但是从未发生过。

...然而

  • 一旦我在Flash CC中停止调试器,工作人员将写入文件。
  • 发布应用并从Windows资源管理器执行后,它可以正常工作(意思是,文件是在后台编写的,而应用程序正在按预期运行)。

这很疯狂。为什么会这样?

感谢。

1 个答案:

答案 0 :(得分:1)

我有一段时间遇到过类似的问题。

我从未设法将跟踪输出传送到调试器。但是我能够在log txt文件中找到worker trace输出: %APPDATA%\Macromedia\Flash Player\Logs\

至于工作人员SWF在调试期间没有进展 - 尝试将工作者SWFS编译为Release,而不是Debug。