AS3 using Workers

2019-09-19 06:15发布

问题:

  • IDE: FLASH CS6 Professional
  • SDK: AIR 18 for Android

I'm trying to use Workers for my app. The problem is, it doesn't work that it suppose to be.

public class Main extends Sprite {

    private var worker:Worker;

    public function Main() {            

        var ba:ByteArray = this.loaderInfo.bytes;

        if(Worker.current.isPrimordial){
            worker = WorkerDomain.current.createWorker(ba);             
            worker.start();
            trace("Created Main");
        }else{
            trace("Created Worker");
        }
    }
}

It should output

Created Main
Created Worker

but i'm only getting

[SWF] Test.swf - 2831 bytes after decompression
Created Main
[UnloadSWF] Test.swf
Test Movie terminated.

EDIT:

Ok i tried to publish the app and ran it. It works like it should be. But why doesn't it work when i try to ran it with Adobe Debug Launcher ?

This is what it looks like on Android Debug Launcher (ADL)

ADL

This is what it looks like when Published.

Published

EDIT 2:

Tried it with Flash Professional CC 2015. Upgraded to AIR SDK 19.0.0.213. Still the same result. What a pain Adobe.

回答1:

It should work, but Flash Pro CS6 doesn't show the worker's trace statements. Check flashlog.txt or send messages from the worker to the main SWF over a MessageChannel.



回答2:

I made a lib to support use of Workers, if you want use you can check it in GitHub, it is open source project.. hope help ASWorker Link

The implementation is like this

package
{
    import com.tavernari.asworker.ASWorker;
    import com.tavernari.asworker.notification.NotificationCenter;
    import com.tavernari.asworker.notification.NotificationCenterEvent;

    import flash.display.Sprite;

    public class ASWorkerDemo extends Sprite
    {
        private var asWorker:ASWorker;

        //BOTH AREA

        public function ASWorkerDemo()
        {
            //important know, all class start here will be replicated in all works.
            asWorker = new ASWorker(this.stage, uiWorkerStartedHandler, backWorkerStartedHandler);  
        }

        //BOTH AREA END

        //UI AREA START
        private function uiWorkerStartedHandler():void{
            //implement all class or calls for your UI

            NotificationCenter.addEventListener("FROM_BACK_EVENT_MESSAGE", onFromBackEventMessageHandler );

        }

        private function onFromBackEventMessageHandler(e:NotificationCenterEvent):void
        {
            trace(e.data);

            if(e.data == "completed job"){
                NotificationCenter.dispatchEventBetweenWorkers( new NotificationCenterEvent("NEXT_MESSAGE") );
            }
        }

        //UI AREA END

        //BACK AREA START

        private function backWorkerStartedHandler():void{
            //implement all class or calls for your BACK operations 
            NotificationCenter.addEventListener("NEXT_MESSAGE", uiCallForNextMessageHandler );
        }

        private function uiCallForNextMessageHandler():void
        {
            for(var i:int = 0; i < 15; ++i){
                NotificationCenter.dispatchEventBetweenWorkers( new NotificationCenterEvent("FROM_BACK_EVENT_MESSAGE", false, i) );
            }

            NotificationCenter.dispatchEventBetweenWorkers( new NotificationCenterEvent("FROM_BACK_EVENT_MESSAGE", false, "completed job") );
        }

        // BACK AREA END
    }
}

Good luck with Workers



回答3:

Air 20.0.0.204 ADL is also not enjoying anything worker related (from my limited experimentation), however the desktop Flash Player seems to work just fine. This makes debugging convoluted and problematic when you are using Air specific libraries in your project and prefer not to wildcard variable types.



回答4:

At the moment, workers are only supported on desktop platform. This Adobe developer's guide page specifically mentions "for desktop platforms".

Meanwhile, there is an AS3-Worker-Compat library by Jeff Ward that enables you to write code with Workers that will still work correctly in an environment that doesn't support Workers (of course, in this case your code will run in single-threaded mode).

I think many people are waiting for Adobe to implement workers for mobile, hopefully it will come in the future.