-1

I am using Flash Builder 4.7 and have created a Worker Class. Below is the code:

package co.fuix.mobile.system.model
{
    import flash.display.DisplayObjectContainer;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.system.MessageChannel;
    import flash.system.Worker;
    import flash.utils.getDefinitionByName;

    import mx.managers.SystemManager;

    public class InstantMessengerWorker extends Sprite
    {

        private var bm:MessageChannel;
        private var mb:MessageChannel;
        public function InstantMessengerWorker()
        {

            super();

            bm = Worker.current.getSharedProperty("BACK_TO_MAIN_CHANNEL");
            mb = Worker.current.getSharedProperty("MAIN_TO_BACK_CHANNEL");
            mb.addEventListener(Event.CHANNEL_MESSAGE, onMainToBack);

        }

        protected function onMainToBack(event:Event):void
        {
            if(mb.messageAvailable){
                var s:SystemManager;
                trace('*'+mb.receive());
                trace('**'+mb.receive());
                trace('***'+mb.receive());
                trace(mx.core.FlexGlobals.topLevelApplication.myVariable);
            }
        }
    }
}

How do I reference a variable in the main mxml file. I know how to use message channels but I want to get that variable straight.

When I run the above code, this part

trace(mx.core.FlexGlobals.topLevelApplication.myVariable);

is giving me an error.

Any help will be regreatly appreciated

ketan
  • 19,129
  • 42
  • 60
  • 98
Drmaposa
  • 13
  • 4

1 Answers1

0

You can't access a variable from the main app like that. They are running separately. What you need to do is:

Link to Adobe docs

Jason Reeves
  • 1,716
  • 1
  • 10
  • 13
  • Thank you @jason-reeves for your answer. At least I now know how it works. Then is it possible to share the FlexGlobals.topLevelApplication like so `worker.setSharedProperty("FlexGlobalsObject", mx.core.FlexGlobals.topLevelApplication);` in the main SWF. Then in the worker I want to access the property as `flexGlobalsProperty = Worker.current.getSharedProperty("FlexGlobalsObject");`. Basically I want to share the FlexGlobals Object between the swf. – Drmaposa Apr 10 '15 at 14:55
  • no that won't work. only simple objects can be shared. Basically you have two different flash players running and sending info back and forth either through a message channel or shared properties. This is stateless and one can't see into the other for references... only pass messages back and forth (except ByteArray which is sharable). The link I gave you is the ONLY way to handle communications to workers. – Jason Reeves Apr 10 '15 at 18:14
  • Thanks Jason. Now I understand. Its only that I started using Workers recently. ;) – Drmaposa Apr 11 '15 at 09:15