Skip to content

Instantly share code, notes, and snippets.

@jgranick
Created August 24, 2012 19:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jgranick/3454468 to your computer and use it in GitHub Desktop.
Save jgranick/3454468 to your computer and use it in GitHub Desktop.
How to make a background worker thread (NME recipe)
import com.eclecticdesignstudio.motion.Actuate;
import cpp.vm.Thread;
import nme.display.Sprite;
import nme.events.Event;
import nme.Lib;
class ThreadingExample extends Sprite {
public function new () {
super ();
var worker = Thread.create (doWork);
worker.sendMessage (Thread.current ());
addEventListener (Event.ENTER_FRAME, this_onEnterFrame);
}
private function doWork ():Void {
var main = Thread.readMessage (true);
for (i in 0...100) {
main.sendMessage ("Progress: " + (i / 100) + "%");
}
main.sendMessage ("Done");
}
private function this_onEnterFrame (event:Event):Void {
var message = Thread.readMessage (false);
if (message == "Done") {
removeEventListener (Event.ENTER_FRAME, this_onEnterFrame);
}
trace (message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment