Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
If you want delayed call you should use Timer, not "infinite" loop. And you don't have to adjust anything to the CPU.
Copy link to clipboard
Copied
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.");
}
Copy link to clipboard
Copied
Ok, thanks for reply.
The premise of my discussion is there has a Class that will access the network, and maybe occupy the CPU for long time. It will return some result in an unspecific long time. So I need a custom asynchronous method. I short my code - like above - use a for loop instead the whole thing for example to test this.
Is there any possible to implement this?
Forgive me my poor English expression.
Sincerely,
Sin-Wi
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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?