Thread: Too much Cpu usage

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932

    Too much Cpu usage

    I cant seem to find why it uses 50% of the CPU.
    Apparently im not getting the handle of the Win_1 because its writing over the window and not in it.

    Thanks for any suggestion!

    Code:
    #include <windows.h>
    #include <tchar.h>
    #include <string>
    #include <iostream>
    #include <richedit.h>
    #pragma comment(lib,"user32.lib")
    #pragma comment(lib,"Gdi32.lib")
    using namespace std;
    
    #define IDB_TEXT2 101
    #define IDB_TEXT3 102
    #define GetF 103
    
    HWND hwnd, Win_1, Win_2, Send;
    MSG Msg;
    HDC hdc;
    PAINTSTRUCT ps;
    
    TCHAR *buf;
    int length;
    size_t found ;
    string Text, str;
    
    void GetIt()
    {
         hdc = BeginPaint(Win_1, &ps);
         length = GetWindowTextLength(Win_2);
         buf = new TCHAR[length+1];
         GetWindowText(GetDlgItem(hwnd,IDB_TEXT3), buf, length+1);
         str   = buf;
         found = str.find("\r\n");
         Text  = str.substr(0,found);
         TextOut(hdc, 10, 10, Text.c_str(), strlen(Text.c_str()));
         EndPaint(Win_1, &ps);
         delete[] buf;
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
      switch(msg){
                  case WM_CREATE:
                       Win_1 = CreateWindowEx(0,"Edit","",
                             WS_VISIBLE | WS_CHILD |  WS_HSCROLL | WS_VSCROLL |
                             ES_AUTOHSCROLL | ES_WANTRETURN | ES_MULTILINE,
                             20, 20, 550, 460,hwnd,(HMENU) IDB_TEXT2,
                             ((LPCREATESTRUCT)lParam)->hInstance,NULL);
                       Win_2 = CreateWindowEx(0,"Edit","hello",
                             WS_VISIBLE | WS_CHILD |  WS_HSCROLL | WS_VSCROLL |
                             ES_AUTOHSCROLL | ES_WANTRETURN | ES_MULTILINE,
                             20, 500, 550, 180,hwnd,(HMENU) IDB_TEXT3,
                             ((LPCREATESTRUCT)lParam)->hInstance,NULL);
                       Send = CreateWindowEx(0,"Button","Send",
                             WS_CHILD | WS_VISIBLE | WS_BORDER,
                             20, 700, 550, 20,hwnd,(HMENU)GetF,0,NULL);
    
                       EnableWindow(Send ,TRUE);
                       break;
                  case WM_PAINT:
                       GetIt();
                       break;
                  case WM_COMMAND:
                       switch(wParam)
                       {
                        case GetF:
                             InvalidateRect(hwnd,0,FALSE);
                             break;
                       }
                  case WM_TIMER:
                       break;
    
                  case WM_CLOSE:
                       DestroyWindow(hwnd);
                       break;
    
                  case WM_DESTROY:
                       PostQuitMessage(0);
                       break;
                  default:
                       return DefWindowProc(hwnd,msg,wParam,lParam);
           }
           return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
    {
        WNDCLASSEX wc;
        wc.hInstance = hInstance;
        wc.cbClsExtra = 0;
        wc.cbSize = sizeof(WNDCLASSEX);
        wc.cbWndExtra = 0;
        wc.hbrBackground = (HBRUSH) CreateSolidBrush(RGB(100,100,100));
        wc.hCursor = LoadCursor(NULL,IDC_ARROW);
        wc.hIcon   = LoadIcon (NULL, IDI_APPLICATION) ;
        wc.lpfnWndProc =  WndProc;
        wc.lpszClassName = "Class";
        wc.lpszMenuName = NULL;
        wc.style = 0;
        wc.hIconSm = NULL;
        if(!RegisterClassEx(&wc))
        {
            MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        hwnd = CreateWindowEx(WS_EX_LEFT|WS_EX_LTRREADING|WS_EX_WINDOWEDGE,
                               "Class",
                               " The_Chatter",
                               WS_OVERLAPPEDWINDOW,
                               CW_USEDEFAULT, CW_USEDEFAULT, 600, 800,
                               HWND_DESKTOP, NULL, hInstance, NULL);
    
        if(hwnd == NULL)
        {
            MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); return 0;
        }
    
        SetTimer(hwnd,1,125,NULL);
        ShowWindow(hwnd, nCmdShow);
        UpdateWindow(hwnd);
    
        while(GetMessage(&Msg, NULL, 0, 0) > 0)
        {
              TranslateMessage(&Msg);
              DispatchMessage(&Msg);
        }
        return Msg.wParam;
    }
    Last edited by Ducky; 10-25-2009 at 05:21 AM.
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    BeginPain will cause the window to redraw, thus sending another WM_PAINT message.
    To quote MSDN:
    "An application should not call BeginPaint except in response to a WM_PAINT message."
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thank you Elysia but i got another program with BeginPaint and it wont use any CPU.

    But on second thought maybe i should use SendMessage() instead of TextOut().

    I tried it with SendMessage() but its not working either and still uses a lot of CPU.


    Code:
    #include <windows.h>
    #include <tchar.h>
    #include <string>
    #include <iostream>
    #include <richedit.h>
    #pragma comment(lib,"user32.lib")
    #pragma comment(lib,"Gdi32.lib")
    using namespace std;
    
    #define IDB_TEXT2 101
    #define IDB_TEXT3 102
    #define GetF 103
    
    HWND hwnd, Win_1, Win_2, Send;
    MSG Msg;
    HDC hdc;
    PAINTSTRUCT ps;
    
    TCHAR *buf;
    int length;
    size_t found ;
    string Text, str;
    
    void GetIt()
    {
         length = GetWindowTextLength(Win_2);
         buf = new TCHAR[length+1];
         GetWindowText(GetDlgItem(hwnd,IDB_TEXT3), buf, length+1);
         str   = buf;
         found = str.find("\r\n");
         Text  = str.substr(0,found);
         SendMessage(Win_1, LB_INSERTSTRING, (WPARAM)-1, (LPARAM)Text.c_str());/*
         hdc = BeginPaint(Win_1, &ps);
         TextOut(hdc, 10, 10, Text.c_str(), strlen(Text.c_str()));
         EndPaint(Win_1, &ps);*/
         delete[] buf;
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
      switch(msg){
                  case WM_CREATE:
                       Win_1 = CreateWindowEx(0,"Edit","",
                             WS_VISIBLE | WS_CHILD |  WS_HSCROLL | WS_VSCROLL |
                             ES_AUTOHSCROLL | ES_WANTRETURN | ES_MULTILINE,
                             20, 20, 550, 460,hwnd,(HMENU) IDB_TEXT2,
                             ((LPCREATESTRUCT)lParam)->hInstance,NULL);
                       Win_2 = CreateWindowEx(0,"Edit","hello",
                             WS_VISIBLE | WS_CHILD |  WS_HSCROLL | WS_VSCROLL |
                             ES_AUTOHSCROLL | ES_WANTRETURN | ES_MULTILINE,
                             20, 500, 550, 180,hwnd,(HMENU) IDB_TEXT3,
                             ((LPCREATESTRUCT)lParam)->hInstance,NULL);
                       Send = CreateWindowEx(0,"Button","Send",
                             WS_CHILD | WS_VISIBLE | WS_BORDER,
                             20, 700, 550, 20,hwnd,(HMENU)GetF,0,NULL);
    
                       EnableWindow(Send ,TRUE);
                       break;
                  case WM_PAINT:
                       GetIt()
                       break;
                  case WM_COMMAND:
                       switch(wParam)
                       {
                        case GetF:
                             //GetIt(hwnd);
                             InvalidateRect(Win_1,0,FALSE);
                             //system("start http://www.google.com");
                             break;
                       }
                  case WM_TIMER:
                      // if (GetAsyncKeyState(VK_F11)) ShowWindow(hwnd,SW_HIDE);
                       //if (GetAsyncKeyState(VK_F12)) ShowWindow(hwnd,SW_SHOW);
                       break;
    
                  case WM_CLOSE:
                       DestroyWindow(hwnd);
                       break;
    
                  case WM_DESTROY:
                       PostQuitMessage(0);
                       break;
                  default:
                       return DefWindowProc(hwnd,msg,wParam,lParam);
           }
           return 0;
    }
    Using Windows 10 with Code Blocks and MingW.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I am really bad at Windows stuff, but I would encourage you to use a profiler to see the bottleneck. There are free ones out there, such as AMD's CodeAnalyst.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thanks, ill check it out.
    Using Windows 10 with Code Blocks and MingW.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    A solution has been submitted to your Win32 newsgroup post concerning this issue.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thank you very much Bob!

    Problem solved, lesson learned!
    Using Windows 10 with Code Blocks and MingW.

  8. #8
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Quote Originally Posted by Elysia View Post
    BeginPain will cause the window to redraw, thus sending another WM_PAINT message.
    To quote MSDN:
    "An application should not call BeginPaint except in response to a WM_PAINT message."
    BeginPaint() does not send any msgs. The values returned are not valid in any other msg than WM_PAINT.

    The issue appears to be using a function pointer to the paint handler (rather than an ID number) when creating the 'Send' control (and a timer firing every 125 ms).
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I misinterpreted MSDN then...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reduce CPU usage
    By patrick22 in forum Windows Programming
    Replies: 9
    Last Post: 07-10-2009, 02:13 PM
  2. questions on multiple thread programming
    By lehe in forum C Programming
    Replies: 11
    Last Post: 03-27-2009, 07:44 AM
  3. Net cpu usage of pthreads?!
    By mynickmynick in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2008, 07:59 AM
  4. Calculating CPU Usage
    By vitaliy in forum Linux Programming
    Replies: 3
    Last Post: 08-21-2005, 09:38 AM
  5. CPU Usage so high
    By X PaYnE X in forum Windows Programming
    Replies: 9
    Last Post: 12-21-2003, 03:07 AM