Last active
July 5, 2020 21:42
-
-
Save guilherme-v/7e8b66fc21d18c4f03db20741b3d0acd to your computer and use it in GitHub Desktop.
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 'dart:async'; | |
import 'dart:io'; | |
import 'dart:isolate'; | |
main(List<String> args) async { | |
Isolate timerIsolate; | |
final mainControlPort = ReceivePort(); | |
// Creates a new isolate | |
// Once it's start, it will run the "timerTick" function and | |
// the param will be the "mainControlPort.sendPort". | |
// This away it can send messages back to the "parent" main Isolate when needed | |
timerIsolate = await Isolate.spawn( | |
timerTick, | |
mainControlPort.sendPort, | |
debugName: "TimerIsolate", | |
); | |
} | |
void timerTick(SendPort mainPort) async { | |
stdout.writeln(Isolate.current.debugName + " started"); | |
Timer.periodic(Duration(seconds: 1), (timer) { | |
final ts = DateTime.now().toIso8601String(); | |
stdout.writeln("sending back to MainIsolate: " + ts); | |
mainPort.send(ts); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment