PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

100% CPU Usage normal?

First of all a quick question: how do I get rid of the input indicator on the bottom of the screen of the PocketPC? I think it's accomplished with an API call, but its name escapes me...

Now, unto more important matter. I have a program that (yet) does not do anything except keep the standard message loop running inside of WinMain:

while ( GetMessage( &msg, NULL, 0,0 ) == TRUE ) {
TranslateMessage (&msg);
DispatchMessage(&msg);
}
return (msg.wParam);

There's some other 'fluff' such as initialisation of the program, but that's all generated by the AppWizard when making a 'HelloWorld' app.

For some bizzare reason, when I run the program, the CPU usage goes up to 100% and stays there 'till I kill it or (if I have key input handling) quit the program internally.

Any ideas as to why it's doing this?
Because you never sleep. Stick a Sleep(10) (or higher) in there after DispatchMessage.

Dan East
To make your app fullscreen, and hide the SIP button, etc., do the following whenever you receive a WM_ACTIVATE message when your app is gaining activation:
[fixed]
SHFullScreen(hWnd, SHFS_HIDETASKBAR|SHFS_HIDESIPBUTTON);
MoveWindow(hWnd, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), TRUE);
[/fixed]

Dan East
You shouldn't need the Sleep() call to yield.  GetMessage() will block when there are no more messages to process and the OS scheduler should switch to another process if one is waiting to run.  You might have something screwed up in your WndProc, or if you have a timer running that is generating messages GetMessage will never block.

How are you determining the current CPU usage?

Thanks for the help.
First problem (100% CPU) was fixed by remarking of the empty 'case WM_PAINT' (well, it's empty @ the moment), though Sleep(x) also worked. Programmers take notice - never leave empty WM_PAINT case! Or you will be wasting CPU without reason.
Second question (full screen) was fixed w/ those 2 API calls. Thanks again.

BTW, any idea as to why an empty case statement (and yes, it did have break in it) would cause the CPU to go to 100%?

And as to how I measured the CPU usage - task manager on win2k ;)
You must call BeginPaint and EndPaint if you handle a WM_PAINT message. Windows will send your app a WM_PAINT if your window's update region is not empty (which indicates which portions of your window need to be redrawn). BeginPaint / EndPaint clears the update region, so Windows will not call WM_PAINT again unless part of the window needs to be redrawn, or you explicitly call RedrawWindow, etc. You can also use ValidateRect to validate your Window's entire client area to prevent more WM_PAINT messages. However, if you don't ever paint in WM_PAINT, then don't even handle the message.

Dan East
Have you considered peekmessage instead of getmessage?
>>And as to how I measured the CPU usage - task manager on win2k  

Duh.  I knew about that.  I was under the impression that you were running your app on a Pocket PC and had some way to get the CPU usage.

You must call BeginPaint and EndPaint if you handle a WM_PAINT message...You can also use ValidateRect to validate your Window's entire client area to prevent more WM_PAINT messages...


I do all of my rendering in the WM_PAINT method, so I need to be able to handle it.
Regarding the Begin and EndPaint: are these called before and after the rendering loop, respectively? And what do I do w/ ValidateRect? Call it after EndPaint to let the program know I'm done? Please clarify...
What's the difference b/w the two?

Have you considered peekmessage instead of getmessage?
Moose or Chuck · 13 Sep 2001, 19:21 UTC
One checks the message queue to see if there is a pending message, but does not remove it (peekmessage), which is faster if you don't expect a lot of messages.
getmessage always tries to retrieve a message from the queue, so it could be slower, especially if you aren't sending/receiving messages often with your program (like a game).
The advantage of PeekMessage is that it doesn't wait (block) for an incoming message if there is no message currently available.  This allows you to test if a message is available, if not, then go do something else (like updating your screen).

Moose,
I never understood the point of calling PeekMessage and not retrieving a message (PM_NOREMOVE) if it was available.  You're going to have to pull the message out anyway, you might as well get it from making one API call instead of two.

If you call BeginPaint and EndPaint then they will clear the invalidated region for you.
The purpose of having an invalidated region is so an app can only redraw parts of the window that need to be redrawn (IE were concealed by another window, then re-exposed). In CE, due to the fact the almost all windows are full-screen, there are few times when your app will be able to refresh only a portion of its main window. If you have to redraw your window regularly (like most games do many times a second), then you really don't even need process WM_PAINT, because you app will be doing the drawing shortly regardless. WM_PAINT is primarily for static windows whose contents are only updated due to user input. Partial redrawing is also very useful if you are creating custom controls, or performing custom drawing on existing controls.
I don't know what type of game you are writing, but if it is graphically intensive, or updates often, then you probably shouldn't be using Windows messages to prompt your drawing (WM_PAINT). But, if what you are doing is lightweight enough that it runs properly on the slower devices, then of course that is fine.

Dan East