Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I can't find a good way to make my app work in the background after the user has closed it (with the X button or from the Apps Bar), a way to keep the process alive in C++.

What I have tried:

I have tried to detect when the user closes the app but it hasn't worked.
Posted
Updated 1-Oct-20 9:48am

Windows provides no easy way to capture messages sent to a console window. If you need the application to run in the background, you can:

1. Create a service (requires Admin rights to install / run / stop it). Your console program can communicate with the service via various methods.
2. Create a Windows application that runs with a minimized window. Your console program can start the Windows application, and communicate with its window via standard Windows messages. The Windows program will continue execution after the console program terminates.
 
Share this answer
 
Processes and threads are different things: A process is a block of memory combined with one or more threads, and an app is part of a process - it's basically a thread.
When your app closes normally, the thread that is executing is terminated, as are all threads it started that are marked as "Background". Non-background thread don't, and the process is only closed and the memory recycled when all it's threads are closed.
If the app is closed abnormally then the process will die with it.

This leaves you two options: start a non-background thread within your app before returning from main, or (better) start a different process from your app and let that run as it will.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900