3 windows, cpu 100%

This is a discussion on 3 windows, cpu 100% within the Windows Programming forums, part of the Platform Specific Boards category; Hello Everyone When I originally created three windows I did not realize that they were making my cpu run at ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    22

    3 windows, cpu 100%

    Hello Everyone
    When I originally created three windows I did not realize that they were making my cpu run at 100% while my program was running. So I had been happily programming away but when I created even more windows with same child child class , my system is sticking almost freezing. So I tracked back through my versions to realize that the child windows must be the cause of the problem. I'm only creating the child windows once in the WM_CREATE part of winproc. I must be missing something obvious.Thanks for any replies.
    phil

    I'm using c
    win2k
    dev c++

    Code:
    /*------------------------------------------------------------
       V3_6
      ------------------------------------------------------------*/
    
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
    LRESULT CALLBACK ChildWndProc (HWND, UINT, WPARAM, LPARAM) ;
    LRESULT CALLBACK ChildChildWndProc (HWND, UINT, WPARAM, LPARAM) ;
    HWND hwndChildChild;         // handle for child of child window
    HWND hwndChild;         // handle for child window
    TCHAR szChildClass[] = TEXT("Main_Child");
    TCHAR szChildChildClass[] = TEXT("Child_Child");
    
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                        PSTR szCmdLine, int iCmdShow)
    {
         static TCHAR szAppName[] = TEXT ("HelloWin") ;
         HWND         hwnd ;
         MSG          msg ;
         BOOL bRet;    // returm of getmessage
    
         WNDCLASS     wndclass ;
    
         wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
         wndclass.lpfnWndProc   = WndProc ;
         wndclass.cbClsExtra    = 0 ;
         wndclass.cbWndExtra    = 0 ;
         wndclass.hInstance     = hInstance ;
         wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
         wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
         wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
         wndclass.lpszMenuName  = NULL ;
         wndclass.lpszClassName = szAppName ;
    
         if (!RegisterClass (&wndclass))
         {
              MessageBox (NULL, TEXT ("This program requires Windows NT!"), 
                          szAppName, MB_ICONERROR) ;
              return 0 ;
         }
         // Update winclass for main_child window .START
         // Then register class .START
         wndclass.lpfnWndProc     = ChildWndProc;
         wndclass.hIcon           = NULL;
         wndclass.hbrBackground   = (HBRUSH) GetStockObject (LTGRAY_BRUSH) ;
         wndclass.lpszClassName   = szChildClass;
         RegisterClass(&wndclass);
         // Update winclass for main_child window .FINISH
         // Then register class .FINISH
    
         // Update winclass for child_child window .START
         // Then register class .START
         wndclass.lpfnWndProc     = ChildChildWndProc;
         wndclass.hIcon           = NULL;
         wndclass.hbrBackground   = (HBRUSH) GetStockObject (GRAY_BRUSH) ;
         wndclass.lpszClassName   = szChildChildClass;
         RegisterClass(&wndclass);
         // Update winclass for child_child window .FINISH
         // Then register class .FINISH
         
         hwnd = CreateWindow (szAppName,                  // window class name
                              TEXT ("The Win Program"), // window caption
                              WS_OVERLAPPEDWINDOW |
                              WS_CLIPCHILDREN ,        // window style
                              CW_USEDEFAULT,              // initial x position
                              CW_USEDEFAULT,              // initial y position
                              CW_USEDEFAULT,              // initial x size
                              CW_USEDEFAULT,              // initial y size
                              NULL,                       // parent window handle
                              NULL,                       // window menu handle
                              hInstance,                  // program instance handle
                              NULL) ;                     // creation parameters
         
         ShowWindow (hwnd, iCmdShow) ;
         UpdateWindow (hwnd) ;
         
         while ((bRet = GetMessage (&msg, NULL, 0, 0))!=0)
         {
              if (bRet == -1)
              {
               MessageBox (NULL, TEXT ("Get message, error!"), 
                          szAppName, MB_ICONERROR) ;
            // handle the error and possibly exit
              }
              else
              {
                TranslateMessage (&msg) ;
                DispatchMessage (&msg) ;
              }
         }
         return msg.wParam ;
    }
    
    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
       
      static int result = 5;
      int iLength;
      TCHAR szBuffer[100];
      HDC         hdc ;
      PAINTSTRUCT ps ;
      RECT        rect ;
             
      switch (message)
      {
        case WM_CREATE:
          
             // Create child window .START
             hwndChild = CreateWindow(szChildClass, NULL,
                                    WS_CHILDWINDOW  | WS_VSCROLL |
                                    WS_CLIPSIBLINGS ,
                                    40,40,360,420,
                                    hwnd, (HMENU) 1001,
                                    (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE),
                                     NULL); 
            ShowWindow (hwndChild, SW_SHOW);
              hwndChildChild = CreateWindow(szChildChildClass, NULL,
                                    WS_CHILDWINDOW | WS_VISIBLE |
                                    WS_CLIPSIBLINGS ,
                                    0,0,20,20,
                                    hwndChild, (HMENU) 1002,
                                    (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE),
                                     NULL);  
                        
             // Create child window .FINISH
          
           return 0 ;
              
        case WM_PAINT:
              
              hdc = BeginPaint (hwnd, &ps) ;
              
              GetClientRect (hwnd, &rect) ;
          
              
              EndPaint (hwnd, &ps) ;
              return 0 ;
        case WM_SIZE:
                  
             return 0;
          
        case WM_DESTROY:
              PostQuitMessage (0) ;
              return 0 ;
      }
        return DefWindowProc (hwnd, message, wParam, lParam) ;
    }
    
    LRESULT CALLBACK ChildWndProc (HWND hwnd, UINT message, 
                                          WPARAM wParam, LPARAM lParam)
    {
     
      switch(message)
      {
      case WM_CREATE:
           
           return 0;
      
      case WM_PAINT:
    
           return 0;
       case WM_SIZE:
           
            return 0;
      }
      return DefWindowProc(hwnd, message, wParam, lParam);
    }
    LRESULT CALLBACK ChildChildWndProc (HWND hwnd, UINT message, 
                                          WPARAM wParam, LPARAM lParam)
    {
    
      switch(message)
      {
      case WM_CREATE:
    
           return 0;
      
      case WM_PAINT:
    
           return 0;
      case WM_SIZE:
           
             return 0;
      }
      return DefWindowProc(hwnd, message, wParam, lParam);
    }

  2. #2
    Banned
    Join Date
    Jun 2005
    Posts
    594
    in your while statment add Sleep(1);

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,659
    Your process is spinning on WM_PAINT because the update region is never cleared for either of the child windows.

    The WM_PAINT Message (second to last paragraph)

    You can use this as your "template" WM_PAINT handler:
    Code:
    case WM_PAINT:
       ValidateRect(hwnd, 0); /* clear/validate update region */
       return 0;
    gg

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    22
    Thank you for the 2 replies, I tried the sleep( ) first, in the while statement and that stopped the cpu running at 100%. Why does this work?

    Then I tried ValidateRect() in WM_PAINT. When I put ValidateRect() in both child winprocs that also worked.
    Now I understand.
    Code:
    case WM_PAINT
          return 0;  // will loop for ever
    Thanks
    phil

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Upgrading my old CPU (for another old one!)
    By foxman in forum Tech Board
    Replies: 16
    Last Post: 01-11-2008, 04:41 PM
  2. how to make a windows application
    By crvenkapa in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2007, 09:59 AM
  3. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 03:44 AM
  4. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 06:05 PM
  5. IE 6 status bar
    By DavidP in forum Tech Board
    Replies: 15
    Last Post: 10-23-2002, 05:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21