![]() |
|
Going background with window messages
By Nicholas Tsipanov, October 22, 2001.
SummaryIt is often needed to run your application in background, invisible to user. This article describes one of possible approaches to the problem solution. PrefaceWhat could you do on desktop if you were asked to create a background process? You can either create a service application (on NT) or create an appliation that has only one WinMain function. Pocket PC lacks NT services like Windows 9' platform so this solution won't do. But we can create an invisible window that activates its work on system timer clicks or window custom messages. Getting startedWe won't need to have a Dialog based application but we need to have support of MFC classes (CString is the most wanted of them). Create a standard MFC dialog based application called BackgroundApp, then eliminate all unnecessary stuff: remove CBackgroundAppDlg.* from project (we will create a CWnd-based window instead) and remove #include "BackgroundAppDlg.h" line. Then change the standard CBackgroundAppApp::InitInstance implementation. You will have something like this: This will compile but won't run because MFC applications cannot run without MainWindow. That's the task we have to accomplish. Within windows - without windowsHere we will create a light-weighted window (comparing to CDialog based windows) that will get its own window procedure: We override CWnd::Create to create this window invisible to user : Let's do something useful on timer events: We had declared a custom window message that we will handle if we need this process to handle our commands. To handle this we have to override WindowProc method: To test this feature create a simple application that either sends a custom message to your invisible window directly or sends it broadcast: Then we will add a manually created invisible window to the application that receives windows messages. (add BackgroundWnd.* to the project before). SampleYou can download a sample project (10Kb) that was used in this article. ConclusionSometimes it is useful to have a background application (e. g. doing some log). One of the approaches is to create an invisible window and spawn it to activate on timer ticks. To make other communications with your window the simplest way is to send it user window messages in the way described in this article. You can develop this application further; for example you can add process dispatcher its processes according to some rules. The other way to communicate is to create use system pipes, semaphores but they are a bit more complicated. Related resources:
DiscussDiscuss this article. Here you can write your comments and read comments of other developers. |
|||||||||||||||||||||