From d500ffa7841d3e7186a21d5dd45072d4d2138e9c Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Sun, 4 Oct 2020 16:42:29 -0400 Subject: [PATCH] implement handler for CTRL-C on windows. --- tools/lammps-shell/lammps-shell.cpp | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/tools/lammps-shell/lammps-shell.cpp b/tools/lammps-shell/lammps-shell.cpp index cfae8ad50b..6dd03cd5bf 100644 --- a/tools/lammps-shell/lammps-shell.cpp +++ b/tools/lammps-shell/lammps-shell.cpp @@ -29,7 +29,6 @@ #if !defined(_WIN32) #include -#else #endif #include @@ -206,12 +205,21 @@ extern "C" { #if !defined(_WIN32) static void ctrl_c_handler(int) -{ - if (lmp) - if (lammps_is_running(lmp)) lammps_force_timeout(lmp); -} #else +static BOOL WINAPI ctrl_c_handler(DWORD event) #endif +{ +#if defined(_WIN32) + if (event == CTRL_C_EVENT) { +#endif + if (lmp) + if (lammps_is_running(lmp)) lammps_force_timeout(lmp); +#if defined(_WIN32) + return TRUE; + } + return FALSE; +#endif +} static char *cmd_generator(const char *text, int state) { @@ -473,6 +481,7 @@ static void init_commands() #if !defined(_WIN32) signal(SIGINT, ctrl_c_handler); #else + SetConsoleCtrlHandler(ctrl_c_handler, TRUE); #endif } @@ -493,6 +502,7 @@ static int shell_end() { write_history(".lammps_history"); if (lmp) lammps_close(lmp); + lammps_mpi_finalize(); lmp = nullptr; return 0; }