2

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.

  1. According to the manuals the default message loop runs only if there is window associated with it.

  2. 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.

  3. I want to run this program like a background program without a window.

  4. I am using a callback function as the last parameter for the setTimer function so i dont have to have a window procedure.

  5. 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.

Jayapal Chandran
  • 10,600
  • 14
  • 66
  • 91
  • Sounds like maybe this should be a Service instead of an Application? – cbranch Nov 05 '12 at 13:49
  • what i am doing is that the system should not go idle (using programming and not by removing the idleness from control panel) so after every 30 idle seconds the timer calls a function which will use a kbd event to press a key ... some thing like this... besides it will also be good to learn how to write servies... i will also try that. – Jayapal Chandran Nov 05 '12 at 14:03
  • The possibility of a -1 return value means that such code can lead to fatal application errors. Instead, check GetMessage on MSDN: http://msdn.microsoft.com/en-us/library/windows/desktop/ms644936.aspx – CS. Jun 03 '13 at 12:33
  • `(TIMERPROC) getidle` is potentially an undefined behavior, see [Casting a function pointer to another type](https://stackoverflow.com/questions/559581/casting-a-function-pointer-to-another-type) – user Oct 21 '14 at 14:06

1 Answers1

3

GetMessage is a blocking call, which means if there are no messages available, it doesn't return. If you have a hidden window that isn't around to receive any messages, the thread that contains the message loop doesn't consume any CPU resources. The one message you want to process is WM_QUIT, which enables a clean exit. You can post this message yourself with PostMessage elsewhere in your code.

If you approach it this way, you'll need to set up your other threads and timers before the message loop, because once GetMessage blocks you won't be able to do anything else.

eh9
  • 7,340
  • 20
  • 43