I am making a console app, and I want my program,to do some cleaning up, after you close its console
I researched a lot but I haven’t found any good answer to this
>Solution :
There are many ways for a process to be terminated—closing the console window is just one. In general, you cannot depend on being able to clean up on close. Alternatives are to clean-as-you-go and/or clean-up leftovers on the next launch.
If you’re worried only about the closing the console situation. Here are a couple options on Windows. Unfortunately, neither of them is trivial to implement.
-
Rather than creating a console app, create a "Windows" app that doesn’t have a GUI and instead creates its own console. When the user closes that console, the process will still be running. I cannot remember if there’s a way to attach your "Windows" program to the console it was started from. If there were, I think Visual Studio would have used that rather than the
devenv.comanddevenv.exetrick. -
Create a console app that launches a second (non-console) program. The second process is your main program, but it to direct its output to the console app (e.g., using a named pipe). Likewise, the console app would have to direct user input to the second process. If the user closes the console (or the console app), the second process can continue to run.