Re: C++ Message Handling without using state machines
"libster" <ohudson274@aol.com> wrote:
I'm writing code and I have a scenario where I'm waiting to receive
a msg and a timeout message at the same time.
That's a little superfluous, isn't it? I mean, a "timeout message"
already IS a "message". So what you really meant is, "I'm waiting
[callback function?] to receive messages, which may be timeout or
other".
For example, I've set a timer so that if I don't receive a message
in a certain time, then I will receive a timeout message. Based on
which message I receive first I will perform certain tasks. Is
there a good way to handle this in C++ without using state machines?
I'd say a function which processes incoming messages and alters the
state of a system based on what messages it receives already IS a
"state machine". Why are you afraid of that term?
If I receive the timeout message first then what do I do when the
other message comes in or vice-versa? Need to determine a way to
find out, once I receive a message, if the other message has been
received yet. Any advice would be greatly appreciated.
Use static bool variables to keep track of whether certain messages
have been received yet by a function. Sort of like:
int
__stdcall
MyCallbackFunction
(
HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam
)
{
static bool GotTimeout = false;
static bool GotSleep = false;
static bool GotWakeup = false;
static bool GotExplode = false;
switch (message)
{
case TIMEOUT:
GotTimeout = true;
// do stuff
return 1;
case SLEEP:
GotSleep = true;
// do stuff
return 1;
case WAKEUP:
GotWakeup = true;
// do stuff
return 1;
case EXPLODE:
bomb.explode();
return 666;
}
return 0;
}
You'll have to decide under what conditions you want to reset
your "got-the-message" booleans so that they can be triggered
by the next message of that type. But I think the above scheme
would work.
--
Cheers,
Robbie Hatley
Tustin, CA, USA
lonewolfintj at pacbell dot net (put "[ciao]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/