Problem with CPU Usage
-
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
-
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
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
-
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
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)
-
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
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)
-
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
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] -
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)
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" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You
-
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)
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
-
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" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You
So, if you have any other suggetion then you can share it.
Ashish Bhatt
-
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
Did you see my suggestion ? What about using GetMessage instead of PeekMessage ?
Cédric Moonen Software developer
Charting control [v1.2]