0

In my current windows 10 version (10.0.18362) it does not work to cancel a python program with Ctrl + C when running in CMD.

I dont know if it has to do with it, I just want to state that the main Thread has finished but another thread is running when I try to hit Ctrl + C.

What can I do to get a KeyboardInterrupt Exception that I urgently need in order to close the socket that is in use?

AKX
  • 152,115
  • 15
  • 115
  • 172
TheProgrammer
  • 23
  • 1
  • 2
  • 2
    Might be relevant since you mention multithreading: https://stackoverflow.com/a/11816038/51685 – AKX Jul 29 '19 at 08:17
  • Some insight on code would help but otherwise, I second @AKX – LazyCoder Jul 29 '19 at 08:21
  • @LazyCoder The code is available here: https://github.com/TheProgrammer21/Python-DHCP – TheProgrammer Jul 29 '19 at 08:25
  • Windows does not implement POSIX signals in the kernel. Programs that are attached to a console can get `CTRL_C_EVENT`, `CTRL_BREAK_EVENT`, and `CTRL_CLOSE_EVENT` on a new thread. The C runtime maps the first to `SIGINT` and the others to `SIGBREAK`. If Python handles a signal, its C handler sets a flag for the interpreter to call the handler in the main thread, so for best results never block the main thread. Python's `SIGINT` handler raises `KeyboardInterrupt`, and `SIGBREAK` is left at the default console control handler that calls `ExitProcess`. – Eryk Sun Jul 29 '19 at 11:35
  • If you can't prevent the main thread from blocking, then you can use ctypes or PyWin32 to install a console control handler that handles `CTRL_C_EVENT` to close the socket and exit. – Eryk Sun Jul 29 '19 at 11:37

0 Answers0