Click to See Complete Forum and Search --> : PeekMessage and GetMessage no good
slippnslide
July 24th, 2006, 10:35 AM
I tried using PeekMessage, but it uses 90% of my CPU, even with a sleep(500) in the code. GetMessage is causing some other problems too:
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
DoNetworkStuff();
}
When I run the program, I need to have a packet sent to a server every time a variable changes, but when I minimize my application to the system tray, DoNetworkStuff() never gets executed until I restore the window and the window is in focus.
So, how can I ensure that DoNetworkStuff() will get executed even when the window isn't in focus?
Bornish
July 24th, 2006, 11:12 AM
Start a new thread that calls DoNetworkStuff... or use PeekMessage within the DoNetworkStuff() function, dispatching possible messages received by the application.
Krishnaa
July 24th, 2006, 11:31 AM
There are thousands of messages posted to window, which GetMessage get's, so your DoNetworkStuff practically gets called everytime, comsuming too much of CPU time.
You should instead try using timers(SetTimer) and call DoNetworkStuff in timer handler.
http://msdn.microsoft.com/library/en-us/winui/winui/windowsuserinterface/windowing/timers/timerreference/timerfunctions/settimer.asp?frame=true
slippnslide
July 24th, 2006, 02:05 PM
Bornish: Thanks, I'll give that a try.
Krishnaa: No, that doesn't work. PeekMessage is the one that causes too much CPU usage. GetMessage doesn't do anything at all when the window loses focus. I've tried timers and they don't work either.
VladimirF
July 24th, 2006, 03:20 PM
...I've tried timers and they don't work either.
Timers don't work? They work for everyone else... :)
slippnslide
July 24th, 2006, 05:07 PM
I used timers with GetTickCount() work fine, but I also need the function to execute when a certain IF statement is true. The GetTickCount() timer and the IF statement don't work while the window is out of focus.
CraigV
July 24th, 2006, 07:20 PM
If your using Winsock, use WSA Events, either in a seperate thread or WSA Events that posts to your window Message Pump in the same thread...
humptydumpty
July 25th, 2006, 12:18 AM
First THing try to avoid using Sleep in your Program. Second thing As Already Suggested that use a New Thread which Will Run always in Background .and this will make a call to your DoNetworkStuff() .but this is also not good to Run a Thread Continiously when you are not doing anything So better when you are not doing anything kill your thread . and when you made some changes to your variable again invoke the thread.
Thanx
JohnCz
July 26th, 2006, 01:43 AM
slippnslide
You have to realize that GetMessage does not return until there is a message in a queue.
You should never call any function from a main message loop.
As suggested before you can start new worker thread and synchronize running using one of the synchronization methods depending on the value of the varuable.
slippnslide
July 26th, 2006, 01:47 AM
With a bit of tweaking, I got timers to work. They didn't quite work how everyone else was doing them, but they work fine now. Thanks for all your replies.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.