using timers in win32api windowless application

Win32 API Add comments

I wanted to write a windowless win32 program with timers. It will run in the background and will trigger some functions at specific time intervals. so followed the following conditions. Which would not consume more cpu time.

  • No Window
  • No Window Class defined
  • It should not be a console application.
  • Is should be a kind of background process yet not a windows service
  • It should not contain while loops and sleep functions which shall overloads the CPU
  • It will have timers

I had been reading discussions which discouraged the use of while loops and sleep functions if you are not going to have a windowed application. I too felt the same cause that will consume more of the CPU usage were as there should be a way to make the instance wait for a message. and at this point i thought of using only the message loop as suggested by experts and it worked.

The code below works well if you are not going to have threads and other stuff.

If you program is going to be standalone and if it is simple then you can go for this. I have provided reference links at the bottom of this post.

Here is the code i used. I used Dev-C++ IDE to compile.

The following code will call the function timerfunc every 20 seconds.

#include <windows.h>
void timerfunc();
int main()
    SetTimer(NULL, 1, 20000, (TIMERPROC) timerfunc);
    MSG msg;
    while (GetMessage (&msg, NULL, 0, 0))
    return 0;
void timerfunc()
//some code here

Here i tested by giving void main instead of some thing like the following and it works. I have to do another discussion related to this cause it had been a long time since i did win32 api programming.

int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil)

Besides, the above is what i researched and did for the least. I will have to explore more and would be updating this page.
so, this is all i have for now.


Leave a Reply

Entries RSS