【问题标题】:How should i use Dart Isolate unhandledExceptionCallback?我应该如何使用 Dart Isolate unhandledExceptionCallback?
【发布时间】:2013-03-23 12:51:51
【问题描述】:

我正在尝试在我的 Dart webapp 中使用 Isolates,但我似乎无法使错误回调参数起作用。 我有一个在 Dartium 中运行的非常基本的代码。

import "dart:isolate";

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

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

除了“主要”之外,我从未见过任何东西。打印在控制台上。我做错了什么还是现在坏了?

【问题讨论】:

  • 据我所知,这还没有实现。
  • 那么现在从 Isolate 捕获和记录消息的最简单方法是什么? Isolates 不能访问 DOM 也不能打印到控制台。我不知道异常是否可以在流上序列化,因为在这种情况下,我可以使用 MessageBox 和 streamSpawnFunction 变体将它们发送到主机代码并在那里打印它们。但这太丑了:(
  • 好吧,这行得通,但它非常丑陋:) 我可能会自己编写一个包装器 Isolate 类。无论如何,我需要双向通信,这已经需要一些帮助代码。我不知道为什么从早期的 Dart 版本中删除 Isolate 类是件好事。自己重新创建流更令人困惑。

标签: dart dart-isolates


【解决方案1】:

错误回调将在新的隔离中执行。因此它不能是动态闭包,而必须是静态函数。

我还没有测试过,但这应该可以:

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);
}

注意:在 dart2js 中,此功能仍未实现。旧版本只是忽略了这个论点。较新的版本会抛出 UnimplementedError。

【讨论】:

    猜你喜欢
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-04
    • 2023-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多