How is CLion terminating a process runing? - c++

I am working on a TCP server that I'm developing on CLion under Windows 10.
I would like my program to be able to end properly, when I click on "stop" my program. I thought CLion was sending a signal, but after trying to catch them all, it looks like it does not.
So my question is, how does CLion stop running the program? Is it possible to detect it within the program?
Thank you in advance.

I found my answer here.
According to this link, signals are actually used :
Click this button to terminate the current process externally by means
of the standard shutdown script. Clicking the button once invokes soft
kill allowing the application to catch the SIGINT event and perform
graceful termination (on Windows, the Ctrl+C event is emulated). After
the button is clicked once, it is replaced with icon run tool window
kill indicating that subsequent click will lead to force termination
of the application, e.g. on Unix SIGKILL is sent.

Related

Windows console application - Is it possible to detect (and handle) the window close (X) button hit?

I don't want to deflect the button hit or ignore the close event. I just need to run some cleanup code.
Let me tell you what I've tried so far:
atexit()
_onexit()
SetConsoleCtrlHandler()
~QCoreApplication()
No code set by these is being executed.
The process just get killed when I press the X.
I certainly can make a windowed app and solve this, but is there a solution for a console application?
Thanks!

click close console window to end a c++ console program is proper way?

I have just got this problem for a few days. Before, I've always thought that letting the program exit by returning from main and clicking close the console window is the same way to end the program.
However, I've found that they are different. Since my program opens a camera which is an object. And closing the console windows does not destroy or clean up the object. So the next time I have error to open the camera again
I just need a confirm if this is true?
Then why only until now I can see the problem?
Closing a console window in Windows, kills the running program (or stack of running programs). Unless it has registered a handler for this event, it gets no chance to clean up. If you want solution, register a handler.
Hm, consulting the documentation, wait a few secs…
OK, look up SetConsoleCtrlHandler.
Closing a running console application will kill the process, not giving you the chance for any clean up code. I guess you could hook a windows message loop to trap the WM_CLOSE message and do proper cleanup, but at the end of the day, you just shouldn't kill the process.

Sending Ctrl+C event to a process launched using QProcess on Windows

I have a dialog which acts as a configurator for a console application. The dialog’s job is to offer the user a set of widgets (which mirror the options supported by the console application) and when user clicks on the “Start” button, the dialog creates and starts a QProcess with the console application’s name and parameters based on the state of the widgets in the GUI. I am able to start the process successfully and everything works fine. However, when I want to kill the process, the console application needs to shutdown gracefully, meaning it has to close files, flush data, close devices etc., and then terminate.
I used QProcess::close(), this immediately kills the application and the app is unable to shutdown gracefully.
I have used the Win32 GenerateConsoleCtrlEvent(CTRL_C_EVENT, Q_PID::dwProcessId) to send an even to the same. I see that the above API returns a non-zero value (indicating a success, it would return 0 upon failure), but my process continues to run.
Can anyone help me with how I can signal the QProcess to shutdown gracefully? Or is there any other way to do this?
GenerateConsoleCtrlEvent takes a process group id, not a process id. You are likely feeding it a process id, since that's what QProcess provides.
QProcess doesn't support creation of a process group at the moment. You need to either start the process manually using winapi, or patch your copy of Qt to amend qtbase/src/corelib/io/qprocess[.h,.cpp,_win.cpp] to pass the CREATE_NEW_PROCESS_GROUP creation flag.
If you don't wish to tweak Qt itself, you can copy the qprocess files to your project, rename the class, and add the changes there.

My Visual FoxPro application appears to close... but is still running in the Task Manager

My FoxPro program has a quit button and the usual min/max/X buttons in the top right, and when using either the program disappears and seems to have closed properly. However, when I check my Task Manager, I find that it is still running.
This is my main.prg file:
ON SHUTDOWN CLEAR EVENTS
with _screen
.visible = .f.
endwith
DO FORM locations\form1
READ EVENTS
ON SHUTDOWN
and this is the code for my exit button 'click' event:
unlock all
close database all
clear events
RELEASE ALL
quit
My program has only the one form and it's set as top-level... any suggestions as to how I can fix this?
Thanks for your time and help :)
If you are running VFP originating from the IDE (Development environment), and you do _Screen.Visible = .F., you are HIDING the main VFP screen, and the system may be returning directly to that, and since you can't see it, you cant formally quit.
In your MAIN.PRG, put the following down at the bottom as a separate "function" that will be visible within the call stack.
function CloseMyApp
*/ For now, just to "ignore" any errors of any dangling objects trying to get released
*/ and otherwise might be HIDING an error upon shutdown.
on error *
*/ NOW, clear the event handler and close everything else down
clear events
close database all
close tables
on shutdown
quit
endfunc
Then, in your start, change your ON SHUTDOWN to call this "function"... Right now, you are only issuing a single command of clear events. This way, you can wrap up a bunch of "cleanup" operations before closing, and not just clearing the read events.
ON SHUTDOWN Do CloseMyApp in Main.PRG
If you click the EXIT button on the form (your button) does the form close and the application close correctly ?
If it does, ASSUMING the button is called 'cmdExit' then add this line to the 'UNLOAD' event of the form
THISFORM.cmdExit.click()

What happens when you close a c++ console application

I guess the question says it all, but, what happens if someone closes a c++ console app? As in, clicks the "x" in the top corner. Does it instantly close? Does it throw some sort of exception? Is it undefined behavior?
Closing a c++ console app with the "x" in the top corner throws an CTRL_CLOSE_EVENT which you could catch and process if you set a control handler using the SetConsoleCtrlHandler function. In there you could override the close functionality and perform whatever you wished to do, and then optionally still perform the default behavior.
I imagine that the console process just gets unceremoniously killed by the OS. If you want to trap this event and do something it looks like the SetConsoleCtrlHandler function is the way to do it.
See also:
How to handle a ctrl-break signal in a command line interface
Console Event Handling
On Linux and other Unix systems, the console runs as a separate process. As you close the shell, it sends the SIGHUP signal to the currently active process or processes that are not executed in the background. If the programmer does not handle it, the process simply terminates. The same signal is sent if you close the SSH session with a terminal and an active process.
SIGBREAK is raised on Windows.