Jump to content
Larry Ullman's Book Forums

How To Wait Till An Asynchronous Process Finishes

Recommended Posts

I am wondering if someone can tell me how to force a wait till an asynchronous process finishes.


Following is the code that I'm having trouble with:

private function stage_1_processing(): void
var inStream:FileStream = new FileStream();
inStream.open(filein, FileMode.READ);
var ba:ByteArray = new ByteArray();
//Transform byteArray to bitmapdata
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, stage_2_processing);
loader.loadBytes(ba); // this is an asynchronous operation!!
private function stage_2_processing(evt:Event): void
trace("Entering stage 2 processing");
var content:* = loader.content;
var bigBMD:BitmapData = new BitmapData(content.width,content.height,true,0x00000000);

I want to force the program to wait until the loader.loadBytes process is finished before continuing on to the stage_2_processing function.


It seems to 'fall through' to the stage_2_processing logic before the asynchronous loader.loadBytes process is finished.


Any assistance/guidance will be much appreciated.


Cheers from Oz.

Link to comment
Share on other sites

Hi HartleySan,


Yes, thanks for the tip.


What was confusing me was that I had three 'alert' displays in the stage_2_processing function and they were displaying in the reverse order than I was expecting!  I seem to recall that I had come across this once before and was never able to figure out why the alerts were displaying in the reverse order.  But display in the reverse order they do, but the code actually executes in the correct, sequential order!


In the end I discovered that the 'bug' in the function was unrelated to the asynchronicity of the loader.loadBytes - embarrassingly I had a simple spelling error!!


Many thanks for your assistance.


Cheers from Oz.

Link to comment
Share on other sites


  • Create New...