Running CWinApp without a window
-
Monday, April 09, 2012 8:37 PM
I'm working on some legacy code for a small program that does all its work inside CWinApp::InitInstance() and then returns FALSE without ever creating a window. The problem is that they want this program to sit in memory and nudge the main program at predetermined intervals (don't ask why, it's too painful a story) which means instead of waking up, doing a couple things, and then dying quietly, it now needs to sit in memory forever.
I've been reading up on what it takes to capture timers in a windowless application, but what do I need to do, so that the app neither terminates nor uses 100% of the CPU? I need to pump the message loop, don't I?
Will it work to call this at the end of InitInstance()?
BOOL bRet;
MSG msg;
while( (bRet = GetMessage( &msg, 0, 0, 0 )) != 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return FALSE; // Return FALSE from InitInstance() so MFC doesn't try to create a window for me
Thanks,
Chris
All Replies
-
Monday, April 09, 2012 8:40 PM
On 4/9/2012 4:37 PM, Chris Shearer Cooper wrote:
I've been reading up on what it takes to capture timers in a
windowless application, but what do I need to do, so that the app
neither terminates nor uses 100% of the CPU?The easiest way is to create a window, but just keep it hidden.
Igor Tandetnik
- Proposed As Answer by Pintu ShuklaMicrosoft Community Contributor Monday, April 09, 2012 11:17 PM
-
Monday, April 09, 2012 11:19 PM
why not simply write a dll.I didn't find any use of any type of window here as your logic is based on timer .Because if you killed your main program what will you do with this timer . So i think creating a dll makes more sense or a simple timer in your app only in a different thread use multi threading here. You don't have to create different window app for this neither with nor without a window
Thanks
Rupesh Shukla
- Edited by Pintu ShuklaMicrosoft Community Contributor Tuesday, April 10, 2012 5:02 AM
- Edited by Pintu ShuklaMicrosoft Community Contributor Tuesday, April 10, 2012 5:02 AM
-
Tuesday, April 10, 2012 3:44 AMModerator
Hi Chris,
If the application in question needs to "hang around", a more appropriate solution would likely be to implement a windows service, instead of a desktop application. Particulary if you need the application to be running regardless if the user is logged in.
Alternatively, you could try implementing a windows app, where the window simply remains hidden.
Sincerely,
Ed Dore
- Proposed As Answer by Ed DoreMicrosoft Employee, Moderator Tuesday, April 10, 2012 3:44 AM
-
Tuesday, April 10, 2012 6:25 AM
Inside InitInstance, instead of creating window, try putting an infinite loop that executes periodical operation followed by Sleep.
-
Tuesday, April 10, 2012 12:34 PM
My concern with an infinite loop is that when the user wants to shut down Windows, I think my app will show up as something that won't exit. So I think I need to keep the message loop pumping so that I can exit gracefully?
-
Tuesday, April 10, 2012 2:18 PM
Yes you need to exit the service shortly after the shut down request is sent by the system or the system would think your process is unresponsive.
You can have worker processes like IIS does if you are doing a lot of work in the service session.
The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
Visual C++ MVP

