7 Replies Latest reply on Nov 14, 2011 10:58 PM by kglad

    A weird problem, how to implement an async method?


      Hi, all. We talk the async method, it most used on network accesses. But now, I have a class that will do several things, including but not limited to network accessing, many many single loops within a huge code(maybe). The return time is unspecific, so I think about using an async method. However, when I run my program, I found that all things in call stack are stuck when the loop running.

      I made a short version of my code. First, the weird AsyncClass.as class is like this:


      package {
                import flash.events.Event;
                import flash.events.EventDispatcher;
                public class AsyncClass extends EventDispatcher {
                          public function AsyncClass() {
                          public function asyncStart():void {
                                    //warning: well, you should change the delay counting number according to your CPU
                                    for (var d:int = 0; d<100000000; d++) {}
                                    dispatchEvent(new Event(Event.COMPLETE));


      and then, within my asyncTest.fla, I placed two buttons with instance name of 'button1' and 'button2'. On the first frame, I wrote these code:


      var asyncObject:AsyncClass = new AsyncClass();
      addEventListener(MouseEvent.CLICK, onClick);
      asyncObject.addEventListener(Event.COMPLETE, onComplete);
      function onClick(event:MouseEvent):void {
                switch (event.target.name) {
                          case 'button1':
                                    trace('start at: '+getTimer());
                          case 'button2':
                                    trace('click at: '+getTimer());
      function onComplete(event:Event):void {
                trace('complete at: '+getTimer());


      As run, I click button1 first, and as quick as possible to click button2, But the output always shows me that complete first, click last. And obviously, it was stuck at the time I clicked button2.

      Any ideas how can I change these code to implement to async? Thank you all!


      sincerely, waiting answer