PDA

View Full Version : high Cpu usage with windows api



Pswin
08-24-2009, 08:14 AM
hi

my cpu usage for MS Windows application is too high ( about 50%)

how can i decrease it?

my code for handling events it: ( i belive problem is in this code)



#ifdef _WIN32
MSG msg;
HWND m_hw = (HWND)m_Window->getHandle();
while(!m_bDone)
{
if ( PeekMessage(&msg, m_hw, NULL, NULL, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
// clearing screen
m_Renderer->clear();
// draw functions

if ( m_Timer->getMilliSeconds() > 1000 )
{
m_uFps = m_uFrameCount ;
m_uFrameCount = 0;
m_Timer->Reset();
std::cout << m_uFps << std::endl;
}
else
{
m_uFrameCount++;
}

// changing frame buffer
m_Renderer->swapFramebuffer();
}
}
#endif

martinsm
08-24-2009, 09:27 AM
Well, you are calling code all the time while messages are unavailable. And if any code is executing, then CPU time is used. Nothing to wonder about.

If you want CPU usage to be lower, then don't render anything for some time (put Sleep(1) affter swapFramebuffer() or enable vsync).

ZbuffeR
08-24-2009, 01:00 PM
Even better, only draw when needed, ie. with WM_PAINT messages.