Created
August 24, 2012 19:05
-
-
Save jgranick/3454468 to your computer and use it in GitHub Desktop.
How to make a background worker thread (NME recipe)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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