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. Problem with CPU Usage

Problem with CPU Usage

Scheduled Pinned Locked Moved C / C++ / MFC
c++visual-studiocomhelpquestion
9 Posts 4 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.
  • A Offline
    A Offline
    ashishbhatt 0
    wrote on last edited by
    #1

    Hi All, I am using MFC Activex Control project in VS 2005. In my project I am doing something like hooking. I have set these hooks from application.In this hooking , I have created some windows procedure(using CALLBACK function) through which I get messages into it contineously(Messages are like when my desktop window changed,cursor position changed,anything something happens on the the screen).For this I have to check contineously for update. And now my problem is like that when I call this hooking function than it starts executing and it does well and here I get all tese messages perfactly but during these constant message transfering my cpu usage goes upto 50%.So, that is very bad for me I want to reduse this cpu overhead near upto 10%.So, do you have any suggetion what to do for that??? I will Appreciate your Answer. Thanks in Advance.

    Ashish Bhatt

    A P 2 Replies Last reply
    0
    • A ashishbhatt 0

      Hi All, I am using MFC Activex Control project in VS 2005. In my project I am doing something like hooking. I have set these hooks from application.In this hooking , I have created some windows procedure(using CALLBACK function) through which I get messages into it contineously(Messages are like when my desktop window changed,cursor position changed,anything something happens on the the screen).For this I have to check contineously for update. And now my problem is like that when I call this hooking function than it starts executing and it does well and here I get all tese messages perfactly but during these constant message transfering my cpu usage goes upto 50%.So, that is very bad for me I want to reduse this cpu overhead near upto 10%.So, do you have any suggetion what to do for that??? I will Appreciate your Answer. Thanks in Advance.

      Ashish Bhatt

      A Offline
      A Offline
      ashishbhatt 0
      wrote on last edited by
      #2

      Here I am using below code to contineously checking for the messages. MSG msg; while(1) { if(!PeekMessage(&msg,m_hwnd,NULL,NULL,PM_REMOVE)) { CheckUpdate(); //MessageBox(NULL,CString("Cannot Peek Message"),CString("RTMPTest"),0); } else if(msg.message == SCREEN_UPDATE) { //MessageBox(NULL,CString("Screen Update Message is Received"),CString("RTMPTest"),0); RECT rect; rect.left = (SHORT)LOWORD(msg.wParam); rect.top = (SHORT)HIWORD(msg.wParam); rect.right = (SHORT)LOWORD(msg.lParam); rect.bottom = (SHORT)HIWORD(msg.lParam); m_region.AddRect(rect); } else if(msg.message == MOUSE_UPDATE) { MessageBox(NULL,CString("Mouse Update Message is Received"),CString("RTMPTest"),0); } else { MessageBox(NULL,CString("Peek UP Message But Not Update Messsage"),CString("RTMPTest"),0); } } Plz do helpful to me if you have any idea. Thanks.

      Ashish Bhatt

      P C 2 Replies Last reply
      0
      • A ashishbhatt 0

        Hi All, I am using MFC Activex Control project in VS 2005. In my project I am doing something like hooking. I have set these hooks from application.In this hooking , I have created some windows procedure(using CALLBACK function) through which I get messages into it contineously(Messages are like when my desktop window changed,cursor position changed,anything something happens on the the screen).For this I have to check contineously for update. And now my problem is like that when I call this hooking function than it starts executing and it does well and here I get all tese messages perfactly but during these constant message transfering my cpu usage goes upto 50%.So, that is very bad for me I want to reduse this cpu overhead near upto 10%.So, do you have any suggetion what to do for that??? I will Appreciate your Answer. Thanks in Advance.

        Ashish Bhatt

        P Offline
        P Offline
        Peter Weyzen
        wrote on last edited by
        #3

        If you're on a dual-core machine -- this 50% is likely 100% for one processor -- which is bad! Have you confirmed this in the debugger? If one of your threads is running at this rate, it should be easy to do a "break all" in the debugger and see what the threads are up to.... If you have a loop that's just going crazy -- you could just stick a sleep in there...

        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc -- PC Power delivered to your phone](http://www.soonr.com)

        1 Reply Last reply
        0
        • A ashishbhatt 0

          Here I am using below code to contineously checking for the messages. MSG msg; while(1) { if(!PeekMessage(&msg,m_hwnd,NULL,NULL,PM_REMOVE)) { CheckUpdate(); //MessageBox(NULL,CString("Cannot Peek Message"),CString("RTMPTest"),0); } else if(msg.message == SCREEN_UPDATE) { //MessageBox(NULL,CString("Screen Update Message is Received"),CString("RTMPTest"),0); RECT rect; rect.left = (SHORT)LOWORD(msg.wParam); rect.top = (SHORT)HIWORD(msg.wParam); rect.right = (SHORT)LOWORD(msg.lParam); rect.bottom = (SHORT)HIWORD(msg.lParam); m_region.AddRect(rect); } else if(msg.message == MOUSE_UPDATE) { MessageBox(NULL,CString("Mouse Update Message is Received"),CString("RTMPTest"),0); } else { MessageBox(NULL,CString("Peek UP Message But Not Update Messsage"),CString("RTMPTest"),0); } } Plz do helpful to me if you have any idea. Thanks.

          Ashish Bhatt

          P Offline
          P Offline
          Peter Weyzen
          wrote on last edited by
          #4

          while (1) { // peek... } Add a Sleep(100) as a simple start...

          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc -- PC Power delivered to your phone](http://www.soonr.com)

          T A 2 Replies Last reply
          0
          • A ashishbhatt 0

            Here I am using below code to contineously checking for the messages. MSG msg; while(1) { if(!PeekMessage(&msg,m_hwnd,NULL,NULL,PM_REMOVE)) { CheckUpdate(); //MessageBox(NULL,CString("Cannot Peek Message"),CString("RTMPTest"),0); } else if(msg.message == SCREEN_UPDATE) { //MessageBox(NULL,CString("Screen Update Message is Received"),CString("RTMPTest"),0); RECT rect; rect.left = (SHORT)LOWORD(msg.wParam); rect.top = (SHORT)HIWORD(msg.wParam); rect.right = (SHORT)LOWORD(msg.lParam); rect.bottom = (SHORT)HIWORD(msg.lParam); m_region.AddRect(rect); } else if(msg.message == MOUSE_UPDATE) { MessageBox(NULL,CString("Mouse Update Message is Received"),CString("RTMPTest"),0); } else { MessageBox(NULL,CString("Peek UP Message But Not Update Messsage"),CString("RTMPTest"),0); } } Plz do helpful to me if you have any idea. Thanks.

            Ashish Bhatt

            C Offline
            C Offline
            Cedric Moonen
            wrote on last edited by
            #5

            Use GetMessage instead of PeekMessage. This way, the GetMessage will block until a message is received in the queue. And you won't need to poll for messages (which consumes CPU cycles). Also, you should check for the WM_QUIT message (and exit your infinite loop) otherwise your application will never terminate.


            Cédric Moonen Software developer
            Charting control [v1.2]

            1 Reply Last reply
            0
            • P Peter Weyzen

              while (1) { // peek... } Add a Sleep(100) as a simple start...

              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc -- PC Power delivered to your phone](http://www.soonr.com)

              T Offline
              T Offline
              ThatsAlok
              wrote on last edited by
              #6

              Peter Weyzen wrote:

              Add a Sleep(100) as a simple start...

              if any message come between that sleep(100) will be lost or delayed to processed by main application, as hook is working here

              "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
              Never mind - my own stupidity is the source of every "problem" - Mixture

              cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You

              A 1 Reply Last reply
              0
              • P Peter Weyzen

                while (1) { // peek... } Add a Sleep(100) as a simple start...

                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc -- PC Power delivered to your phone](http://www.soonr.com)

                A Offline
                A Offline
                ashishbhatt 0
                wrote on last edited by
                #7

                Thanks for reply. i used sleep() before Peekmessage() and it works.But is there no any other way?. And also I want to ask one question that is it right way to check current process cpu usage from task manager?? because I have heard that it is not perfact way. Thanks.

                Ashish Bhatt

                C 1 Reply Last reply
                0
                • T ThatsAlok

                  Peter Weyzen wrote:

                  Add a Sleep(100) as a simple start...

                  if any message come between that sleep(100) will be lost or delayed to processed by main application, as hook is working here

                  "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
                  Never mind - my own stupidity is the source of every "problem" - Mixture

                  cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You

                  A Offline
                  A Offline
                  ashishbhatt 0
                  wrote on last edited by
                  #8

                  So, if you have any other suggetion then you can share it.

                  Ashish Bhatt

                  1 Reply Last reply
                  0
                  • A ashishbhatt 0

                    Thanks for reply. i used sleep() before Peekmessage() and it works.But is there no any other way?. And also I want to ask one question that is it right way to check current process cpu usage from task manager?? because I have heard that it is not perfact way. Thanks.

                    Ashish Bhatt

                    C Offline
                    C Offline
                    Cedric Moonen
                    wrote on last edited by
                    #9

                    Did you see my suggestion ? What about using GetMessage instead of PeekMessage ?


                    Cédric Moonen Software developer
                    Charting control [v1.2]

                    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