PDA

View Full Version : wait for an event?


Ocean
07-14-2008, 06:56 AM
Can anyone explain to me a way to make a function 'wait' until receives a particular event before continuing?

I have a function which receives the path to an XML file, then it processes the XML data, performs a number of calculations, then adds the resulting data to an object.

eg.

private function analyse():void{
//1.
processXML("XML/datafile.xml");
//2.
//further analysis with results of processXML()...after processXML is complete

}


processXML() sets off a chain of functions that perform different analysis . So what I'd like to do is dispatch an event in the last function of that chain that triggers analyse() to continue to step 2. I'd prefer not to have step 2 in another function called by an event listener if at all possible. Is there any way to make a function 'wait' for an event like this?


Sorry, this is probably a simple question, but I'm having trouble getting my head around whether it's something I can achieve with events.


Thanks,

Ocean

amarghosh
07-14-2008, 08:23 AM
if i understand the question correctly:
no matter to whatever depth the method call goes, it will return and automatically continue at step 2... u don't need to do anything in particular.... thats the way control flows... :)

Ocean
07-14-2008, 09:43 AM
Sorry, I probably didn't explain so well. That's usually the way the control flow goes. However, processXML() loads the actual XML file data, then has a listener that calls a different function to process the data. But it only does this after receiving a completion event to say the XML data is 100% loaded.

This breaks the normal control flow and the analyse() function continues without waiting for the data processing to be completed. Which is why I'm trying to find some way to make analyse() wait until it receives an event to show that the data has been processes.

The reason I want this is I have a class which preforms many calculations and throughout the calculations it needs to pull data from XML files to fill variables. ideally I'd like to achieve this in the calculation:

calculations...

var objectToFillWithData:Object = getDataFromXML("XML/desiredData.xml")

further calculations using the data from objectToFillWithData...


But I can't have it continuing to calculate before the the variables are populated and it's not really viable to have lots of step by step functions triggered by event handlers at the end of each step.

Don't know if this is any clearer or whether this is even possible?

senocular
07-14-2008, 02:41 PM
You have to break up the function into two functions and have the second part called once everything else is complete.

Ocean
07-14-2008, 04:12 PM
ok, thanks. That's what I wanted to avoid doing because it makes with code very messy for what I'm trying to do. But doesn't look like there's a way around it..on to plan B. Thanks for you help.