scope(exit) and Ctrl-C

codephantom me at noyb.com
Sat Dec 2 03:52:19 UTC 2017


On Saturday, 2 December 2017 at 00:41:19 UTC, Wanderer wrote:
> Is there any method to cleanup on Ctrl-C?

// ----------------------------------
import std.stdio;
import core.thread;

extern(C) void signal(int sig, void function(int));
extern(C) void exit(int exit_val);

extern(C) void handle(int sig)
{
     writeln("Control-C was pressed..aborting 
program....goodbye...");
     // do more stuff?
     exit(-1);
}

void main()
{
     enum SIGINT = 2;
     signal(SIGINT,&handle);

     scope (exit){ writeln("Cleanup"); }

     writeln("Waiting...");
     Thread.sleep(10.seconds);
     writeln("Done waiting...");
}
// ---------------------------------



More information about the Digitalmars-d-learn mailing list