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

AS3 Worker not functioning properly in Flash CC debug or release, except when published

今天我尝试用 AS3 Worker 做一些后台工作 class。

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

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

然后是worker的这个文件(这个文件的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 构建和启动应用程序时遇到的问题

然而...

这太疯狂了。为什么会这样?

谢谢。

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

我从未设法将跟踪输出通过管道传输到调试器。但是我能够在此处的日志 txt 文件中找到工作人员跟踪输出: %APPDATA%\Macromedia\Flash Player\Logs\

至于 worker SWF 在调试期间没有推进 - 尝试将您的 worker SWFS 编译为 Release,而不是 Debug。