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;
while( (bRet = GetMessage( &msg, 0, 0, 0 )) != 0)
return FALSE; // Return FALSE from InitInstance() so MFC doesn't try to create a window for me
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.
- 已建议为答案 Pintu ShuklaMicrosoft Community Contributor 2012年4月9日 23:17
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
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.
- 已建议为答案 Ed DoreMicrosoft Employee, Moderator 2012年4月10日 3:44
Inside InitInstance, instead of creating window, try putting an infinite loop that executes periodical operation followed by Sleep.
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?
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