Last active
May 28, 2019 07:18
-
-
Save plateaukao/b4e5fd32f2a39d5161584d4a6818b53c to your computer and use it in GitHub Desktop.
isolate_example
This file contains 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
Isolate isolate; | |
void startRealAsyncTask() async { | |
// need a ReceivePort to receive messages. | |
ReceivePort receivePort= ReceivePort(); | |
isolate = await Isolate.spawn(heavyTask, receivePort.sendPort); | |
receivePort.listen((data) { | |
stdout.write('RECEIVE: ' + data + ', '); | |
}); | |
} | |
void heavyTask(SendPort sendPort) { | |
// doing something very heavy here. | |
String msg = "I'm done"; | |
sendPort.send(msg); | |
} | |
void stop() { | |
if (isolate != null) { | |
stdout.writeln('killing isolate'); | |
isolate.kill(priority: Isolate.immediate); | |
isolate = null; | |
} | |
} | |
void main() async { | |
stdout.writeln('spawning isolate...'); | |
await startRealAsyncTask(); | |
stdout.writeln('press enter key to quit...'); | |
await stdin.first; | |
stop(); | |
stdout.writeln('goodbye!'); | |
exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment