UPDATE: Seems this post keeps getting traffic but it was written in 2010 so I just thought I’d point to this post which provides a better answer now: http://stackoverflow.com/questions/10811183/multithreading-in-actionscript-3

Just a quick technical post. Maybe it might help someone or even maybe someone will tell me how it could be improved.

It’s quite annoying that there’s no multi-threading in AS3. I’ve been used to work in an environment that was allowing it so having to step down is disorienting a bit. There is however a way to fake multi-threading. If you have a better method let me know but so far this one seem to work fine for what I needed (this is the new method used in The Fae’s Wyrd inventory system as we have quite some data to process in there).

Assume you have a class name multithreading in which you want to to do something intensive that you don’t want the interface to wait for (sorry about the crappy formatting of the blog).

——————————

import flash.events.TimerEvent;
import flash.utils.Timer;

public class multithreading
{
private var _timerProcess:Timer
private var _processIndex:uint

private var _someArray:Array

public function multithreading()
{
_timerProcess= new Timer(1, 0)
_timerProcess.addEventListener(TimerEvent.TIMER, function():void { doProcess(); } )
}

public function startProcess()
{
if (_timerProcess.running == true) { _timerProcess.stop() }

_processIndex = 0
_timerProcess.start()
}

private function doProcess()
{
if (_processIndex < _someArray.length)
{
// Do something with the data in _someArray

_processIndex++
}
else
{
_timerProcess.stop()
}
}
}

Share