0

With many UPS in case of power failure there is the chance to start a script to be executed before shutting down the system.

In my Qt application I would like to detect some kind of event I can trigger from a batch script and start a procedure to safely stop my application (controlling some HW) before shutting down the host PC. Which is a clean way to make so? How can I listen for such "external" event from my application? and how can I trigger the event itself on Win10?

nimig
  • 155
  • 1
  • 11

2 Answers2

1

I think there is a number of options to do this like sending a signal through a TCP socket, a Windows message to a process etc... The simplest may be to simply send a kill signal to the process and handle it in your application.

To do this I would probably have a look at this: Trapping and handling taskkill in Windows Python. If you taskkill your app from your batch script, you may want to handle WM_CLOSE or CTRL_CLOSE_EVENT as the answer explains, and then execute your cleanup code.

For other Unix like OSes this would be even simpler: send one of the regular signals like SIGTERM and handle it with https://man7.org/linux/man-pages/man2/signal.2.html.

Luca Carlon
  • 9,546
  • 13
  • 59
  • 91
0

Running on the PC there is a Shutdown agent that will catch the signal sent by the UPS in case of power failure. With the Agent a script can be started before shutdown and a time to wait can be defined. I think to use the exit code of the script to catch the shutdown request from the application and to handle it using QProcess:

QProcess process;
process.start("path/to/myscript.bat");
QObject::connect(&process, SIGNAL(finished(int , QProcess::ExitStatus)), this, SLOT(onShutdownRequest(int , QProcess::ExitStatus)));

here the SLOT onShutdownRequest will take care of the clean shutdown. I just need to define a big enough value to wait from the shutdown request to the actual shutdown.

nimig
  • 155
  • 1
  • 11