CPU Intensive App
-
I found a very useful guide to Windows programming from the ground up here. I followed it pretty closely, changing around a little bit by functionizing the initialization. The problem is when I run my app, it consumes all CPU power but I don't have it doing anything when the window pops up, just a window with a menu on top. Can someone go through that page and tell me if I or he missed something to create this problem? Thanks in advance.
-
I found a very useful guide to Windows programming from the ground up here. I followed it pretty closely, changing around a little bit by functionizing the initialization. The problem is when I run my app, it consumes all CPU power but I don't have it doing anything when the window pops up, just a window with a menu on top. Can someone go through that page and tell me if I or he missed something to create this problem? Thanks in advance.
Without seeing what code you actually put in place, it's kind of hard to say what might be wrong.
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
-
Without seeing what code you actually put in place, it's kind of hard to say what might be wrong.
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
I tried to not put code on here because of it's size, but if it must be done. Here's all I wrote, it creates a simple white window.
#define WIN32_LEAN_AND_MEAN #include <windows.h> #include <windowsx.h> LRESULT CALLBACK MessageHandler (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) { return DefWindowProc (hWnd, Msg, wParam, lParam); } ATOM InitializeWindowClass (HINSTANCE hInstance) { WNDCLASSEX wndClass; wndClass.cbSize = sizeof(WNDCLASSEX); wndClass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW; wndClass.lpfnWndProc = MessageHandler; wndClass.cbClsExtra = 0; wndClass.cbWndExtra = 0; wndClass.hInstance = hInstance; wndClass.hIcon = LoadIcon (NULL, IDI_WINLOGO); wndClass.hCursor = LoadCursor (NULL, IDC_ARROW); wndClass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH); wndClass.lpszMenuName = NULL; wndClass.lpszClassName = "Win32 Test"; wndClass.hIconSm = LoadIcon (NULL, IDI_WINLOGO); return RegisterClassEx(&wndClass); } HWND CreateRootWindow (HINSTANCE hInstance) { HWND hWnd = CreateWindowEx(NULL, "Win32 Test", "Sample Window", WS_POPUP | WS_VISIBLE, 300, 300, 800, 600, NULL, NULL, hInstance, NULL); return hWnd; } int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { LPMSG msg = NULL; // initialization stuff goes here InitializeWindowClass (hInstance); HWND hWnd = CreateRootWindow (hInstance); while (true) { // check the message queue if (PeekMessage(msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(msg); DispatchMessage(msg); } // main program logic goes here }
-
I tried to not put code on here because of it's size, but if it must be done. Here's all I wrote, it creates a simple white window.
#define WIN32_LEAN_AND_MEAN #include <windows.h> #include <windowsx.h> LRESULT CALLBACK MessageHandler (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) { return DefWindowProc (hWnd, Msg, wParam, lParam); } ATOM InitializeWindowClass (HINSTANCE hInstance) { WNDCLASSEX wndClass; wndClass.cbSize = sizeof(WNDCLASSEX); wndClass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW; wndClass.lpfnWndProc = MessageHandler; wndClass.cbClsExtra = 0; wndClass.cbWndExtra = 0; wndClass.hInstance = hInstance; wndClass.hIcon = LoadIcon (NULL, IDI_WINLOGO); wndClass.hCursor = LoadCursor (NULL, IDC_ARROW); wndClass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH); wndClass.lpszMenuName = NULL; wndClass.lpszClassName = "Win32 Test"; wndClass.hIconSm = LoadIcon (NULL, IDI_WINLOGO); return RegisterClassEx(&wndClass); } HWND CreateRootWindow (HINSTANCE hInstance) { HWND hWnd = CreateWindowEx(NULL, "Win32 Test", "Sample Window", WS_POPUP | WS_VISIBLE, 300, 300, 800, 600, NULL, NULL, hInstance, NULL); return hWnd; } int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { LPMSG msg = NULL; // initialization stuff goes here InitializeWindowClass (hInstance); HWND hWnd = CreateRootWindow (hInstance); while (true) { // check the message queue if (PeekMessage(msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(msg); DispatchMessage(msg); } // main program logic goes here }
maybe you sould take a look at WaitMessage(VOID);
-
maybe you sould take a look at WaitMessage(VOID);
I had a suspicion something like that was there but I just couldn't find it. Thanks. Now to try to figure out the menus.
-
I tried to not put code on here because of it's size, but if it must be done. Here's all I wrote, it creates a simple white window.
#define WIN32_LEAN_AND_MEAN #include <windows.h> #include <windowsx.h> LRESULT CALLBACK MessageHandler (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) { return DefWindowProc (hWnd, Msg, wParam, lParam); } ATOM InitializeWindowClass (HINSTANCE hInstance) { WNDCLASSEX wndClass; wndClass.cbSize = sizeof(WNDCLASSEX); wndClass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW; wndClass.lpfnWndProc = MessageHandler; wndClass.cbClsExtra = 0; wndClass.cbWndExtra = 0; wndClass.hInstance = hInstance; wndClass.hIcon = LoadIcon (NULL, IDI_WINLOGO); wndClass.hCursor = LoadCursor (NULL, IDC_ARROW); wndClass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH); wndClass.lpszMenuName = NULL; wndClass.lpszClassName = "Win32 Test"; wndClass.hIconSm = LoadIcon (NULL, IDI_WINLOGO); return RegisterClassEx(&wndClass); } HWND CreateRootWindow (HINSTANCE hInstance) { HWND hWnd = CreateWindowEx(NULL, "Win32 Test", "Sample Window", WS_POPUP | WS_VISIBLE, 300, 300, 800, 600, NULL, NULL, hInstance, NULL); return hWnd; } int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { LPMSG msg = NULL; // initialization stuff goes here InitializeWindowClass (hInstance); HWND hWnd = CreateRootWindow (hInstance); while (true) { // check the message queue if (PeekMessage(msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(msg); DispatchMessage(msg); } // main program logic goes here }
-
Try replacing:
while( true ) { if( PeekMessage(...) )
withwhile( GetMessage(...) )
GetMessage stops the program for running while waiting for a message, while PeekMessage doesn't, and you have a loop running at full speed. HTHGood call, I was wary of the `while(true)' loop.
-
I tried to not put code on here because of it's size, but if it must be done. Here's all I wrote, it creates a simple white window.
#define WIN32_LEAN_AND_MEAN #include <windows.h> #include <windowsx.h> LRESULT CALLBACK MessageHandler (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) { return DefWindowProc (hWnd, Msg, wParam, lParam); } ATOM InitializeWindowClass (HINSTANCE hInstance) { WNDCLASSEX wndClass; wndClass.cbSize = sizeof(WNDCLASSEX); wndClass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW; wndClass.lpfnWndProc = MessageHandler; wndClass.cbClsExtra = 0; wndClass.cbWndExtra = 0; wndClass.hInstance = hInstance; wndClass.hIcon = LoadIcon (NULL, IDI_WINLOGO); wndClass.hCursor = LoadCursor (NULL, IDC_ARROW); wndClass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH); wndClass.lpszMenuName = NULL; wndClass.lpszClassName = "Win32 Test"; wndClass.hIconSm = LoadIcon (NULL, IDI_WINLOGO); return RegisterClassEx(&wndClass); } HWND CreateRootWindow (HINSTANCE hInstance) { HWND hWnd = CreateWindowEx(NULL, "Win32 Test", "Sample Window", WS_POPUP | WS_VISIBLE, 300, 300, 800, 600, NULL, NULL, hInstance, NULL); return hWnd; } int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { LPMSG msg = NULL; // initialization stuff goes here InitializeWindowClass (hInstance); HWND hWnd = CreateRootWindow (hInstance); while (true) { // check the message queue if (PeekMessage(msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(msg); DispatchMessage(msg); } // main program logic goes here }
Shouldn't that message loop be:
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(msg);
DispatchMessage(msg);
}
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
-
Shouldn't that message loop be:
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(msg);
DispatchMessage(msg);
}
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
Yes, as long as this window message handling loop is not responsible for any dialog boxes. Otherwise, and you need to know the dialog box's window handle: while (GetMessage(&msg, NULL, 0, 0)){ if( !IsDialogMessage(hWndDialogBox, &msg) ){ TranslateMessage(&msg); DispatchMessage (&msg); } } You also might want to break if the message is WM_QUIT. Now, the new and improved suggestions, from MSDN is: BOOL bRet; while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0) { if (bRet == -1) { // handle the error and possibly exit } else { if( !IsDialogMessage(hWndDialogBox, &msg) ){ TranslateMessage(&msg); DispatchMessage (&msg); } } } :)