我对dart编辑器非常困惑,它是如何工作的。当我运行这个应用程序时
import 'dart:isolate';
import 'package:dbcrypt/dbcrypt.dart';
import 'dart:async';
main() {
//ReceivePort receivePort = new ReceivePort();
var receivePortPw = new ReceivePort();
receivePortPw.listen((msg) {
print(msg);
});
Future<Isolate> f = Isolate.spawn(ReturnHashedPassword, receivePortPw.sendPort);
f.then((Isolate i) {
print('Print1 -> ' + new DBCrypt().hashpw('Password', new DBCrypt().gensalt()));
print('Print2 -> ' + new DBCrypt().hashpw('Password', new DBCrypt().gensalt()));
});
}
void ReturnHashedPassword(SendPort sendPort)
{
print('ok');
ReceivePort receivePort = new ReceivePort();
sendPort.send('Isolate -> ' + new DBCrypt().hashpw('Password', new DBCrypt().gensalt()));
print('done');
}
编辑看起来,它仍然在运行。看看下面的打印屏幕,终止按钮(红色方块按钮)没有禁用,它记得我,当我运行一个http服务器时,这个按钮不会禁用,直到我手动执行它。
为什么终止按钮在输出后不会禁用?它只是I / O应用程序,它不是网络服务器。
答案 0 :(得分:2)
你正在听跑步隔离。你可以关闭它的端口或简单地杀死它:
import 'dart:isolate';
import 'package:dbcrypt/dbcrypt.dart';
import 'dart:async';
main() {
//ReceivePort receivePort = new ReceivePort();
var receivePortPw = new ReceivePort();
receivePortPw.listen((msg) {
print(msg);
receivePortPw.close();//stop listening.
});
Future<Isolate> f = Isolate.spawn(ReturnHashedPassword, receivePortPw.sendPort);
f.then((Isolate i) {
print('Print1 -> ' + new DBCrypt().hashpw('Password', new DBCrypt().gensalt()));
print('Print2 -> ' + new DBCrypt().hashpw('Password', new DBCrypt().gensalt()));
//i.kill(); // not nice...
});
}
void ReturnHashedPassword(SendPort sendPort)
{
print('ok');
ReceivePort receivePort = new ReceivePort();
sendPort.send('Isolate -> ' + new DBCrypt().hashpw('Password', new DBCrypt().gensalt()));
print('done');
}