How to end an isolate and how to get notified when the isolate was terminated

Issue

terminate isolate

When I want to end an isolate launched on the server created using Isolate.spawn() or Isolate.spawnUri()
how can I terminate the isolate.

throw x inside an isolate terminates the isolate when the exception is uncaught.
But I hope there is a more elegant solution.

isolate.kill() throws Uncaught Error: Unsupported operation: kill

get notified

I guess when no more async operations are active the isolate ends (similar to main) but how can I know if the isolate has actually terminated?

Is there a way for the parent isolate to get notified when the child isolate terminates?
isolate.addOnExitListener(_sendPort); throws Uncaught Error: Unsupported operation: addOnExitListener

?

Are there alternative ways or do I have to wait until these methods are supported?

Solution

from https://code.google.com/p/dart/issues/detail?id=10977#c2

The way to detect that an Isolate terminates after it has been
successfully created is to use Isolate.addOnExitListener (possibly
also Isolate.addErrorListener if needing to know why it
terminated). Start the isolate paused, then when the Future
of Isolate.spawnUri completes you can set up listeners and then
resume the isolate.

So we have a way, it is still not implemented by the VM/Dartium, but
that’s another issue.

See also

Answered By – Günter Zöchbauer

Answer Checked By – Senaida (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *