🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.windows.ms.programmer
Article #1391

Re: PeekMessage() question.

#1391
From: mguyott
Date: Tue, 26 Feb 1991 18:05
103 lines
4892 bytes
For those who are interested ...  Here are some additional responses I
received via email regarding the problem I was experiencing with
PeekMessage().  My thanks to everyone who responded!  You're help is greatly
appreciated.                  Marc

My original question was:

>I would like to use PeekMessage() in a yield loop so that other applications
>will be able to run while my application is doing some serious number
>crunching.  However, I have one concern and that has to do with the
>WM_QUIT message.  Will PeekMessage() remove a WM_QUIT message from the
>windows message queue?  If it does can I requeue the message?  Perhaps
>via PostMessage()?

----
Two of the worst things we teach our children are that a knowledge of science
is nice but not necessary, and a knowledge of sex is necessary but not nice.

Marc Guyott              Constellation Software, Inc.          (508) 620-2800
                         Framingham, Mass. 01701 USA                Ext. 3135
mguyott@primerd.prime.com       ...!{uunet, decwrl}!primerd.prime.com!mguyott
----
Subject: Re: PeekMessage() loop.
From: rafetmad@oxy.edu (David Ronald Giller)
To: mguyott@eriador.Prime.COM
Organization: Occidental College, Los Angeles, CA  90041

I use a PeekMessage() loop in one of my programs as the main loop, without
any problems.  Here is the loop section:

// Main Message Loop:----------------------------------------------------------

while (TRUE)
   {
   if (PeekMessage(&msg,
      NULL,
      NULL,
      NULL,
      PM_REMOVE))
	{
	if (msg.message == WM_QUIT)  // WM_QUIT not checked as special case,
	    break;		     // must check explicitly

	TranslateMessage(&msg); // Translates virtual key codes
	DispatchMessage(&msg);	// Dispatches message to appropriate window
	}
   else
	{
	CheckPlayingSong(hWnd);
	}
   }
return (msg.wParam);  // Return the value from PostQuitMessage

//-----------------------------------------------------------------------------

This works (or seems to :-) exactly the same as a GetMessage() loop with the
exception that whenever there is free CPU time, the function CheckPlayingSong()
is performed.  I BELIEVE that this is the only way to do this.

Hope this helps.

-Dave

David Giller ----- (rafetmad@oxy.edu) or (dgiller@oxy.edu) ------------ Box 134
"Some of us wake up -- others roll over."		     Occidental College
"It's easy to deceive a child." -- John Lydon		  Los Angeles, CA 90041
----
Subject: Re: PeekMessage() loop.
From: David Ronald Giller <rafetmad%oxy.edu@RELAY.CS.NET>
Organization: Occidental College, Los Angeles, CA  90041

I don't think you will have to worry too much about the problem with WM_QUIT
(although I could be wrong... I'm no expert!) because of this:	Your MainWndProc
is always reentrant.  That means, if you are processing some information, and
the user wants to exit, and you recieve the WM_QUIT in the middle of your data-
processing-free-time-via-PeekMessage()-loop (knowatimean?) you can simply
kill that 'process' by terminating the app with a processing of the WM_QUIT.

Here's a better explanation.  If you use the SendMessage() function, your
MainWndProc will be stopped where it is, and re-entered (if you sent yourself
a message) with the message you sent.  This could be useful if you, say, had
both a menu item and a control to perform a certain task.  The menuitem
condition would take care of the actual work.  When you push the button, you
might want to show some indicator of what is going on, like setting some
static field on the screen, then performing the action, then setting the
indicator back.  well, you would do this by setting the indicator, calling
a SendMessage(hThisWnd, WM_COMMAND, IDM_WHATEVERTHEACTIONIS, wParam, lParam),
then setting the indicator back.

If you need to finish the computation you are doing, you could set a flag during
the loop where the PeekMessage occurs, then at the end of your process, send
yourself a WM_QUIT.  if you just post one in the PeekMessage section, you may
find yourself with infinite loop problems.

-Good luck, and I hope this helps you.

-Dave

David Giller ----- (rafetmad@oxy.edu) or (dgiller@oxy.edu) ------------ Box 134
"Some of us wake up -- others roll over."		     Occidental College
"It's easy to deceive a child." -- John Lydon		  Los Angeles, CA 90041
----
Technical Details
Message-ID: <149000006@eriador>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!asuvax!ncar!elroy.jpl.nasa.gov!usc!snorkelwacker.mit.edu!primerd!primerd!eriador!mguyott
References: <149000004@eriador>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!asuvax!ncar!elroy.jpl.nasa.gov!usc!snorkelwacker.mit.edu!primerd!primerd!eriador!mguyott
From: mguyott@eriador.prime.com
Newsgroups: comp.windows.ms.programmer
Subject: Re: PeekMessage() question.
Message-ID: <149000006@eriador>
Date: 26 Feb 91 18:05:19 GMT
References: <149000004@eriador>
Lines: 103
Nf-ID: #R:eriador:149000004:eriador:149000006:000:4300
Nf-From: eriador.prime.com!mguyott    Feb 26 09:39:00 1991