Why dart editor is still running

2019-08-09 03:42发布

I am very confuse about dart editor, how it works. When i run this application

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

the editor looks after, it is still running. Look at following printscreen, the terminate button(red square button) is not disable, it remember me, when i run a http server, this button is not gonna disable, until i manually do it. enter image description here

Why the terminate button is not gonna disable here after output? It is only I/O application, it is not a webserver.

1条回答
何必那么认真
2楼-- · 2019-08-09 03:58

You are listening running isolate. You can close it's port or simply kill it:

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');
}
查看更多
登录 后发表回答