Skip to content

Instantly share code, notes, and snippets.

@guilherme-v
Last active July 5, 2020 21:42
Show Gist options
  • Save guilherme-v/7e8b66fc21d18c4f03db20741b3d0acd to your computer and use it in GitHub Desktop.
Save guilherme-v/7e8b66fc21d18c4f03db20741b3d0acd to your computer and use it in GitHub Desktop.
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