CPU usage 100%
-
I have been using a simple routine to wait while connected hardware is performing a reset, ... The routine is:
DWORD stop_time = GetTickCount() + iCount; while ((long)(GetTickCount() - stop_time) < 0L){ MSG msg; if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } }
Someone has complained that the task manager in XP is showing that the CPU usage goes to 100% during this wait period. Do I need to add a sleep in there or something else to prevent this? -
I have been using a simple routine to wait while connected hardware is performing a reset, ... The routine is:
DWORD stop_time = GetTickCount() + iCount; while ((long)(GetTickCount() - stop_time) < 0L){ MSG msg; if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } }
Someone has complained that the task manager in XP is showing that the CPU usage goes to 100% during this wait period. Do I need to add a sleep in there or something else to prevent this?Yes.
PeekMessage
does not wait for a message to appear on the message queue before returning. Deus caritas est -
I have been using a simple routine to wait while connected hardware is performing a reset, ... The routine is:
DWORD stop_time = GetTickCount() + iCount; while ((long)(GetTickCount() - stop_time) < 0L){ MSG msg; if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } }
Someone has complained that the task manager in XP is showing that the CPU usage goes to 100% during this wait period. Do I need to add a sleep in there or something else to prevent this?Instead doing a busy wait try something like this:
UINT_PTR tid = SetTimer(NULL, 1, 5000, NULL); MSG m; while (GetMessage(&m, NULL, 0, 0)) { TranslateMessage(&m); DispatchMessage(&m); if ( m.message==WM_TIMER && m.wParam==1 ) { break; } } KillTimer(NULL, tid);
Steve -
I have been using a simple routine to wait while connected hardware is performing a reset, ... The routine is:
DWORD stop_time = GetTickCount() + iCount; while ((long)(GetTickCount() - stop_time) < 0L){ MSG msg; if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } }
Someone has complained that the task manager in XP is showing that the CPU usage goes to 100% during this wait period. Do I need to add a sleep in there or something else to prevent this?Check Raymond Chen's blog[^], he recently posted about waiting for messages while also having a timeout. --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | NEW!! PimpFish | CP SearchBar v3.0 | C++ Forum FAQ