今天我尝试使用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构建和启动应用程序时遇到的问题 :
worker.state
将确认)。...然而
这很疯狂。为什么会这样?
感谢。
答案 0 :(得分:1)
我有一段时间遇到过类似的问题。
我从未设法将跟踪输出传送到调试器。但是我能够在log txt文件中找到worker trace输出:
%APPDATA%\Macromedia\Flash Player\Logs\
至于工作人员SWF在调试期间没有进展 - 尝试将工作者SWFS编译为Release,而不是Debug。