我一直在玩Dart Isolatesisolate.pause();
,但在使用该功能时遇到了问题。
import 'dart:io';
import 'dart:isolate';
main(){
ReceivePort receivePort = new ReceivePort();
Isolate.spawn(isolateEntryPoint, receivePort.sendPort).then((isolate){
isolate.pause(isolate.pauseCapability);
});
}
void isolateEntryPoint(SendPort sendPort){
while(true){
print("isolate is running");
sleep(new Duration(seconds: 2));
}
}
在我的示例中,隔离基本上只是每 2 秒打印一次。
根据我在docs上阅读的内容,我的理解是上面的代码应该:
- 产生一个隔离
- 立即暂停隔离
但它不起作用,即使我告诉它暂停,隔离仍在运行,并且每 2 秒打印一次“隔离正在运行”。
我知道您可以通过传入可选参数
以暂停状态启动隔离: 。但最终,我希望能够在任何时候暂停隔离,而不是立即暂停。paused: true
Isolate.spawn(isolateEntryPoint, receivePort, paused: true)...
我能找到的关于使用它的唯一文档是官方 dart 文档,所以我可能isolate.pause()
错误地使用了这个函数。但无论哪种方式,我们都将非常感谢演示此函数正确用法的代码示例。