Skip navigation
sinwi
Currently Being Moderated

A weird problem, how to implement an async method?

Nov 14, 2011 8:18 AM

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());
                              asyncObject.asyncStart();
                              break;
                    case 'button2':
                              trace('click at: '+getTimer());
                              break;
          }
}
 
function onComplete(event:Event):void {
          trace('complete at: '+getTimer());
          trace('----------');
}

 

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

Sin-Wi

 
Replies
  • kglad
    72,209 posts
    Jul 21, 2002
    Currently Being Moderated
    Nov 14, 2011 8:26 AM   in reply to sinwi

    i'm not sure what you're trying to do with asyncStart() but that code should be removed.  no matter what you're trying to do, asyncStart() is problematic and won't accomplish anything useful.

     

    then explain what you're trying to accomplish.

     
    |
    Mark as:
  • Peter Celuch
    505 posts
    Nov 17, 2005
    Currently Being Moderated
    Nov 14, 2011 8:30 AM   in reply to sinwi

    If you want delayed call you should use Timer, not "infinite" loop. And you don't have to adjust anything to the CPU.

     
    |
    Mark as:
  • Peter Celuch
    505 posts
    Nov 17, 2005
    Currently Being Moderated
    Nov 14, 2011 8:29 AM   in reply to sinwi
    var myDelayInMiliseconds:Number = 5000;
    var timer:Timer = new Timer(myDelayInMiliseconds);
    timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerCompleteHandler);
     
    timer.start();
    trace("This happens immediately...");
     
    function timerCompleteHandler(event:TimerEvent):void {
         trace("...and this happens after 5 seconds.");
    }
    
     
    |
    Mark as:
  • Currently Being Moderated
    Nov 14, 2011 6:17 PM   in reply to sinwi

    Its nice that you're thinking out of the box for this problem, but this is a situation where you only need to look at your box and what is in it.

     

    Adobe offers many solutions in network communication (webservice, sockets, etc).  Based on what you described above - what you really need are EventListeners.  EventListeners in Flash are also (semi) Async as they are triggered by internal action calls and not by code calls. 

     

    Simple solution:

    That being siad, if you put a listener on your service for when data is received, then no matter what the time frame to receive the data - the event is only triggered when the data is received - then triggering your listening method.

     

    If you explain what service method you are using to communicate with the network, then its easier to provide a working example.

     

    You do not need Async methods for this as its a bit of an over kill just to process Strings.

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 14, 2011 8:59 PM   in reply to sinwi

    Here is a great tutorial about the subject:

     

    http://www.senocular.com/flash/tutorials/asyncoperations/

     

    Don't know if any of this will help with what you are actually trying to do.

     
    |
    Mark as:
  • kglad
    72,209 posts
    Jul 21, 2002
    Currently Being Moderated
    Nov 14, 2011 10:58 PM   in reply to sinwi

    flash isn't going to respond if the cpu is consumed performing some operations.  on the other hand, network communication doesn't consume cpu.

     

    presumably there's some that will occur (sooner or later) via the network.  when, whatever you're waiting for has completed, dispatch your event. 

     

    if you don't understand that last sentence, explain what you're waiting for?

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points