How to write a program with no window, with no window class, with no cpu consuming while loops, no console yet using the message loop which will consume less cpu?
I am explaining my requirements below.
I am writing a win32 api program which uses timer and which does not need a window.
According to the manuals the default message loop runs only if there is window associated with it.
I gone through other stackoverflow content which clearly states to prevent while loop and sleep functions which will cause too much load to the cpu.
I want to run this program like a background program without a window.
I am using a callback function as the last parameter for the setTimer function so i dont have to have a window procedure.
i heard about event/objects to use in the default message loop which will keep the program running yet will not overload the cpu. is this the only way? yet i dont know how to use this option.
What are all the possible ways to write this kind of app without costing the CPU and other resources?
Your suggestions and references would really help.
Here is the code i have now which will exit because no windows is associated as said by other discussions.
#include <windows.h>
void getidle();
int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil)
{
SetTimer(NULL, 1, 5000, (TIMERPROC) getidle);
MSG msg;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
void getidle()
{
//some code here
}
So i need to use a different kind of message loop and i am not sure of how to use it. something like MsgWaitForMultipleObjects or WaitForSingleObject . ? .
Besides, by doing the above mentioned way like msgwait... waitforsigle... or event objects if and only if these could consume more resources that just hiding the window then please specify that also cause i will go with that simple way...
It is working
I really wonder how it works cause i read people saying it will not run. I was searching the net for more information and it had been working... I don't know.
Is it ok? is it a proper way? confused.