【问题标题】:Is there a way to kill tasks created by the compute function?有没有办法杀死由计算功能创建的任务?
【发布时间】:2020-03-06 09:34:35
【问题描述】:

我知道compute function 基于Isolate API。对于 Isolate,您可以通过调用 islate 对象的kill 方法请求关闭 lsolate。

compute 函数能否手动关闭运行此任务的 Isolate?

【问题讨论】:

  • 不,你不能使用compute()顶级函数来做到这一点

标签: flutter dart dart-isolates


【解决方案1】:

没有

不可能杀死compute function

为什么

原因是看the source code of the compute function,创建的isolate只有在result完成后才被杀死:

final Completer<R> result = Completer<R>();
...
await result.future;
...
isolate.kill(); // Always awaits the result.

仅当出现错误或传递给compute 的函数返回时,结果才会完成。


此外,您确实没有有权访问isolate 您自己,因为它是created inside of the compute function

解决方案

如果您希望能够杀死您启动的隔离,请不要使用compute。相反,您必须自己创建Isolate

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-03
    • 2020-06-10
    • 1970-01-01
    • 2011-07-26
    • 2022-07-15
    • 2021-05-07
    • 2021-11-04
    • 1970-01-01
    相关资源
    最近更新 更多