tags:

views:

450

answers:

3

I am creating an action script library.I am calling some APIs which parses some xml and gets me the result. It dispatches an Event.COMPLETE when the parsing is done. I want to monitor whether this event is dispatched in some while loop like "while(eventnotdispatched)" is it possible? I know the other way would be to addeventlistener. But please let me know if the other thing is possible.

Thanks

+1  A: 

EDIT: Davr is right, you would not be able to use the while loop like this. You would need a timer.

Yes, it is possible to poll for it. BUT you will still need to create an event listener. It will work something like this:

private var loadCompleted = false;
private var timer:Timer= new Timer(1);

private function onInitCompleted(event:Event):void
{

    timer.addEventListener(TimerEvent.TIMER, timerHandler);
    timer.start();
}

private function loadCompleteEventHandler(event:Event):void
{
    loadCompleted = true;
    ...
}

private function timerHandler()
{
    if(!loadCompleted)
    {
        ... // stop the timer or something.
        timer.stop();
    }
}

Please note, this is VERY BAD code. I would NEVER use it in production because Actionscript is a event driven language. There should be absolutely NO REASON for you to need to do this. Whatever you are trying to do could be accomplished using another method much simpler. Tell me what you are trying to accomplish with this and I will present a better solution.

Sorry for yelling, it's late and I am sleepy.

CookieOfFortune
+1 for telling him it's a bad idea, -1 for thinking it might work. actionscript is single-threaded, your code would never work as you intend.
davr
yes, you're right, it wouldn't, blame the lack of sleep. I made edits to use a timer.
CookieOfFortune
A: 

Doing that means forcing a synchronous model of execution on the underlying asynchronous model (that works with callbacks).

What are you trying to achieve exactly, and why not use a callback?

Assaf Lavie
+3  A: 

NO, it is not possible. Actionscript is single threaded. Thus while you are waiting in your while loop, that is the only thread running, and the process you are waiting for can never complete. This is why everything is done with events, so that's what you should use. If you need to update your display periodically while you are waiting for something to complete...again, use events. Create a Timer object which generates a TIMER event every so often, and use that to make your updates.

davr