2015-09-02 71 views
0

更新:顯然,這是某種計時問題。在下面的代碼中,我通過點擊處理程序創建了工作程序,但是如果我從Main構造函數創建工作程序(如文檔示例所示),commandChannel消息會傳遞給工作人員 - 即使在任何情況下我都在執行完全相同的事情:創建工人,聽取RUNNING狀態,然後通過commandChannel發送消息。但是,如果我從Main構造函數創建worker,而第一條消息只能通過,而稍後的消息(某些未定義的時間在RUNNING之後)總能通過。爲什麼? 如果我不能依靠WorkerState.RUNNING瞭解何時可以向工作人員發送消息,我如何知道工人何時準備好接收消息?工作人員在RUNNING狀態後沒有收到消息,但確實晚了


我基本上有確切的同樣的事情documentation example,但它不工作。我的設置是:

  • Flash Pro中CC
  • AIR 17桌面
  • 的Windows 7

的問題是,工人似乎不會通過收到的消息渠道獲得的消息,雖然它可以通過外發消息通道成功發送消息到主SWF。

這是我的主類:

public class Main extends MovieClip { 

    [Embed(source="TestWorker.swf", mimeType="application/octet-stream")] 
    private var WorkerSWF:Class; 
    private var workerBytes:ByteArray; 

    private var worker:Worker; 
    private var commandChannel:MessageChannel; 
    private var resultChannel:MessageChannel; 

    public function Main() { 
     workerBytes = new WorkerSWF(); 
     stage.addEventListener(MouseEvent.CLICK, clickHandler); 
    } 

    private function clickHandler(e:MouseEvent):void { 
     stage.removeEventListener(MouseEvent.CLICK, clickHandler); 
     startWorker(); 
    } 

    private function startWorker():void { 
     trace("Start worker"); 

     worker = WorkerDomain.current.createWorker(workerBytes, true); 

     commandChannel = Worker.current.createMessageChannel(worker); 
     worker.setSharedProperty("commands", commandChannel); 

     resultChannel = worker.createMessageChannel(Worker.current); 
     resultChannel.addEventListener(Event.CHANNEL_MESSAGE, resultMessageHandler); 
     worker.setSharedProperty("results", resultChannel); 

     worker.addEventListener(Event.WORKER_STATE, workerStateHandler); 
     worker.start(); 
    } 

    private function workerStateHandler(e:Event):void { 
     trace("Worker state:", worker.state); 

     if(worker.state == WorkerState.RUNNING) { 
      trace("Worker running"); 
      commandChannel.send("START PLEASE"); 
     } 
    } 

    private function resultMessageHandler(e:Event):void { 
     trace(e, resultChannel.receive()) 
    } 
} 

我的工人SWF類:

public class TestWorker extends Sprite { 

    private var commandChannel:MessageChannel; 
    private var resultChannel:MessageChannel; 

    public function TestWorker() { 
     trace("isPrimordial:", Worker.current.isPrimordial) 

     commandChannel = Worker.current.getSharedProperty("commands") as MessageChannel; 
     commandChannel.addEventListener(Event.CHANNEL_MESSAGE, commandMessageHandler); 

     resultChannel = Worker.current.getSharedProperty("results") as MessageChannel; 

     resultChannel.send("Hello from worker"); 
    } 

    private function commandMessageHandler(e:Event):void { 
     trace("commandMessageHandler") 

     //var message:String = commandChannel.receive(); 

     resultChannel.send("Whatever."); 

     setInterval(respond, 1000); 
    } 

    private function respond(){ 
     resultChannel.send("Whatever " + new Date()); 
    } 

} 

的問題是,工人的commandMessageHandler永遠不會觸發。我知道這一點,因爲我從來沒有收到回到主SWF的「無論」消息。 「來自工作者的問候」消息確實會返回到主SWF,因此這是主要工作者消息通道的特定情況。怎麼了?

此外,主SWF並未捕獲來自worker的任何trace操作或運行時錯誤。如何調試工作人員SWF?

回答

0

This seems to be a known issue (see point 2)WorkerState.RUNNING有時在worker的構造函數實際運行之前被調度。所以在我的情況下,從click處理程序中,第一條消息是在worker的構造函數添加了偵聽器來接收它之前發送的。

所以我打算用簡單的解決方法:當工作人員加載時,發送一條消息到主線程,在主構造函數結尾處顯示「我準備好了」。在主線程中,在發送intial命令之前,先聽取它而不是RUNNING

相關問題