How to catch SIGINT for the current in Dart?

帅比萌擦擦* 提交于 2019-12-01 05:29:49

问题


How can Ctrl+C or SIGINT be caught in a Dart program for the current process?

Something similar to this for Node:

process.on('SIGINT', function() {
    // do stuff
});

回答1:


I found the following test code at Unified Diff: tests/standalone/io/signals_test_script.dart

import "dart:io";

void main(args) {
  int usr1Count = int.parse(args[0]);
  int usr2Count = int.parse(args[1]);
  var sub1;
  var sub2;
  void check() {
    if (usr1Count < 0 || usr2Count < 0) exit(1);
    if (usr1Count == 0 && usr2Count == 0) {
      sub1.cancel();
      sub2.cancel();
    }
    print("ready");
  }
  sub1 = ProcessSignal.SIGUSR1.watch().listen((signal) {
    if (signal != ProcessSignal.SIGUSR1) exit(1);
    usr1Count--;
    check();
  });
  sub2 = ProcessSignal.SIGUSR2.watch().listen((signal) {
    if (signal != ProcessSignal.SIGUSR2) exit(1);
    usr2Count--;
    check();
  });
  check();
}


来源:https://stackoverflow.com/questions/18448306/how-to-catch-sigint-for-the-current-in-dart

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