Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CPU Intensive App

CPU Intensive App

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LighthouseJ
    wrote on last edited by
    #1

    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.

    D 1 Reply Last reply
    0
    • L LighthouseJ

      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.

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      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

      L 1 Reply Last reply
      0
      • D David Crow

        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

        L Offline
        L Offline
        LighthouseJ
        wrote on last edited by
        #3

        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 }

        M P D 3 Replies Last reply
        0
        • L LighthouseJ

          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 }

          M Offline
          M Offline
          Max Santos
          wrote on last edited by
          #4

          maybe you sould take a look at WaitMessage(VOID);

          L 1 Reply Last reply
          0
          • M Max Santos

            maybe you sould take a look at WaitMessage(VOID);

            L Offline
            L Offline
            LighthouseJ
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • L LighthouseJ

              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 }

              P Offline
              P Offline
              pablo_mag
              wrote on last edited by
              #6

              Try replacing: while( true ) { if( PeekMessage(...) ) with while( 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. HTH

              L 1 Reply Last reply
              0
              • P pablo_mag

                Try replacing: while( true ) { if( PeekMessage(...) ) with while( 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. HTH

                L Offline
                L Offline
                LighthouseJ
                wrote on last edited by
                #7

                Good call, I was wary of the `while(true)' loop.

                1 Reply Last reply
                0
                • L LighthouseJ

                  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 }

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  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

                  B 1 Reply Last reply
                  0
                  • D David Crow

                    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

                    B Offline
                    B Offline
                    Blake Miller
                    wrote on last edited by
                    #9

                    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); } } } :)

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups