11

Is there a way to do something just before a Dart application running on the VM exits?

I'm writing a server application, so there is no specific point in time where my application will exit. I'm trying to talk to another process, but if a user closes my application (with ctr+c), or it crashes for some unknown reason, I would like to be able to tell that process to close as well.

It would be nice if something similar to this existed:

void main() {
  onExit().then(() {
    process.kill();
  });
}

Thanks.

Edit:

I figured out how to detect Ctr+C:

void main() {
  ProcessSignal.SIGINT.watch().listen((ProcessSignal signal) {
    print("exiting");
    exit(0);
  });
}

Now if the application is ran from the Dart editor, is there a way to detect that closing?

Michael Peterson
  • 348
  • 6
  • 22
  • http://stackoverflow.com/questions/18448306/how-to-catch-sigint-for-the-current-in-dart – Nathaniel Johnson Jan 08 '14 at 03:17
  • Thanks, but would you mind explaining how to use that to capture the ctr+c please? I'm just honestly not sure what's going on in that answer. – Michael Peterson Jan 08 '14 at 04:26
  • Nevermind, I figured that out. Is there a way to detect closing in other situations though? – Michael Peterson Jan 08 '14 at 04:40
  • Most crashes will throw an exception. Try wrapping your entire main() method with a try/catch and in the catch add your shutdown code. – Nathaniel Johnson Jan 08 '14 at 05:36
  • There was issue with sockets that didn't throw an exception, but I got that fixed now (I think). What I'm more wondering about now is if there is a way to detect the dart editor closing the app. That way I wouldn't have to run it from the command line if I don't have to. – Michael Peterson Jan 08 '14 at 06:26
  • I am sure the Dart Editor closes it via a POSIX signal. See @Günter Zöchbauer answer below. The real issue is that Dart is still really in an Alpha state. I love Dart but I wouldn't do anything at the production level in it yet. – Nathaniel Johnson Jan 08 '14 at 15:09

1 Answers1

2

Seems work in progress [POSIX signal handling (SIGINT, SIGTERM, SIGHUP, etc] (http://code.google.com/p/dart/issues/detail?id=15188)

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567