0

I'm trying to use Isolates in my Dart webapp but i can't seem to make the error callback argument work. I have a very basic code which is being run in Dartium.

import "dart:isolate";

void main() {
  print("Main.");
  spawnFunction(test, (IsolateUnhandledException e) {
    print(e);
  });
}

void test() {
  throw "Ooops.";
}

I never see anything than "Main." printed on the console. Am i doing something wrong or is this broken right now?

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
NagyI
  • 5,907
  • 8
  • 55
  • 83
  • As far as I know, this is not implemented yet. – Ladicek Mar 23 '13 at 13:21
  • Then what's the easiest way to catch and log messages from an Isolate right now? Isolates can't access DOM nor print to the console. I don't know if exceptions are serializable on a stream because in that case i can send them to the host code using a MessageBox and the streamSpawnFunction variant and print them there. But this is ugly as hell :( – NagyI Mar 23 '13 at 13:32
  • Okay this works but it's till very ugly :) I might write a wrapper Isolate class on my own. I need bidirectional communication anyway which already requires some helper code. I don't know why was it good to remove the Isolate class from earlier Dart versions anyway. It's a lot more confusing to recreate the streams on my own. – NagyI Mar 23 '13 at 14:22

1 Answers1

2

The error-callback will be executed in the new isolate. Therefore it cannot be a dynamic closure but needs to be a static function.

I haven't tested it, but this should work:

import "dart:isolate";

bool errorHandler(IsolateUnhandledException e) {
  print(e);
  return true;
}

void test() {
  throw "Ooops.";
}

void main() {
  // Make sure the program doesn't terminate immediately by keeping a
  // ReceivePort open. It will never stop now, but at least you should
  // see the error in the other isolate now.
  var port = new ReceivePort();
  print("Main.");
  spawnFunction(test, errorHandler);
}

Note: In dart2js this feature is still unimplemented. Older versions just ignored the argument. Newer versions will throw with an UnimplementedError.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
Florian Loitsch
  • 7,698
  • 25
  • 30