C Board  

Go Back   C Board > Platform Specific Boards > Windows Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-25-2009, 05:08 AM   #1
Registered User
 
Join Date: Dec 2007
Location: France
Posts: 396
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;
}
__________________
Using Code::Blocks,MingW with Windows.

Last edited by Ducky; 10-25-2009 at 05:21 AM.
Ducky is offline   Reply With Quote
Old 10-25-2009, 06:21 AM   #2
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
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."
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 10-25-2009, 06:47 AM   #3
Registered User
 
Join Date: Dec 2007
Location: France
Posts: 396
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 Code::Blocks,MingW with Windows.
Ducky is offline   Reply With Quote
Old 10-25-2009, 06:50 AM   #4
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
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.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 10-25-2009, 10:07 AM   #5
Registered User
 
Join Date: Dec 2007
Location: France
Posts: 396
Thanks, ill check it out.
__________________
Using Code::Blocks,MingW with Windows.
Ducky is offline   Reply With Quote
Old 10-29-2009, 05:03 AM   #6
Registered User
 
Join Date: Mar 2005
Location: Mountaintop, Pa
Posts: 1,054
A solution has been submitted to your Win32 newsgroup post concerning this issue.
BobS0327 is offline   Reply With Quote
Old 10-29-2009, 12:54 PM   #7
Registered User
 
Join Date: Dec 2007
Location: France
Posts: 396
Thank you very much Bob!

Problem solved, lesson learned!
__________________
Using Code::Blocks,MingW with Windows.
Ducky is offline   Reply With Quote
Old 10-29-2009, 11:12 PM   #8
train spotter
 
Join Date: Aug 2001
Location: near a computer
Posts: 3,359
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
novacain is offline   Reply With Quote
Old 10-30-2009, 03:12 AM   #9
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
I misinterpreted MSDN then...
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 02:23 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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