Why dart editor is still running

本小妞迷上赌 提交于 2019-12-11 09:27:57

问题


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.

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


回答1:


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


来源:https://stackoverflow.com/questions/24470247/why-dart-editor-is-still-running

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!