Processing Without Blocking Execution in ActionScript 3.0
I've been working on a project visualizing large datasets and had hit a familiar wall. I needed to parse row upon row of CSV data and it was causing the application to hang up or give the "this script is causing your computer to run slowly" message.
Since this isn't the first time I've encountered this situation, I decided to create a class for asynchronous parsing. AsynchronousProcessor lets you specify a processing step function to call repeatedly and a per-frame time limit.
The step function is then called over and over again until the time limit is reached. When the Flash Player moves to the next frame, the function is called several more times until the time limit hits again. This repeats for each frame until the step method returns true.
So instead of doing all the processing in one frame, it's divided across multiple frames, allowing other actions or animations to occur.
Using the Asynchronous Processor
The AsynchronousProcessor class requires a reference to the Stage and since DisplayObjects not in the display list don't have this reference, I'm using a small StageUtils class instead. This class simply stores a static reference to the Stage.
// set the stage variable used by the AsynchronousProcessor. this // statement could go in the main timeline StageUtils.stage = stage; // define the function that does parsing. when parsing is complete // the method should return true function parseNextLine():Boolean { var line:String = getLine(); if (line == null) { // no more lines to parse return true; } else { parseLine(line); return false; } } // create a processor that calls parseNextLine repeatedly until complete var processor:AsynchronousProcessor = new AsynchronousProcessor(parseNextLine, 400); processor.start();
That's it. Comment below if you have any problems.
- Asynchronous Processor source code (free download)
Delicious
Digg
Reddit
Facebook
Google
Yahoo
Technorati

Comments
Hey Keenan,
Yeah, it's a bit of a tragedy and I do hope threads show up in the next couple versions. Thanks for the link to Alex Harui's solution. Looks a bit more complicated but it has a more elegant way of acquiring an instance of the
Stage, though it will only work in Flex.Hi,
great, this will come in handy quite often! One question: couldn't one work around the "passing the stage reference" issue by using a Timer? Any principal problems with that?
Hi Moritz,
Good suggestion. The timer method would definitely work, especially if you grab the framerate and use that to determine the interval. Although, if there is other heavy processing going on, using the stage's enter frame event will account for that and effectively slow down the asynchronous processing. A timer with a fixed interval on the other hand will keep processing at the same rate, possibly bogging down the player.
I doubt there are too many situations where that would be a problem, though.
I'm working on a PseudoThread class, that's inspired by the Java "Thread" class, which I just learned about IN class. Since there's so much activity in the Flash community, it's inevitable that whatever project I work on, someone else is or already finished solving that same problem. And I just discovered this blog entry of yours. Cue the sad violins.
I like reinventing the wheel, though. :-) When I finish my implementation, I'll upload and show you what I put together, if it's noteworthy. Thanks for releasing yours to the Flash community, that's the spirit!
Hi Rezmason,
Reinventing the wheel can be a good lesson, yes. I'll look forward to seeing your code listing because there's more than one way to solve a problem!
Post new comment