Click to See Complete Forum and Search --> : MsgWaitForMultipleObjects issue


manoj.m
March 27th, 2006, 08:47 AM
hai, i have one problem.
i just invoked a thread and i want to wait until the thread is over as well as no more keys are pressed. So i invoked MsgWaitForMultipleObjects with the following parameters.

HWND h = hwnd;
DWORD res = MsgWaitForMultipleObjects ( 1, &h, false, INFINITE, QS_INPUT );

According to MSDN it should return res = WAIT_OBJECT_0; But i am getting abs(difference) as 1 only. What i am missing in this,
Thanks in advance.

wildfrog
March 27th, 2006, 09:35 AM
But i am getting abs(difference) as 1 only.You are getting what?

MsgWaitForMultipleObjects expects a handle to an event object, you're giving it a HWND.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/msgwaitformultipleobjects.asp

- petter

manoj.m
March 27th, 2006, 11:41 PM
Sorry, i misstyped it, actually it is HANDLE only,
the difference b/w 'res' and WAIT_OBJECT_0 is '1' always, i think it must be the index of the handle.

wildfrog
March 28th, 2006, 03:01 AM
If there are messages posted to the threads message queue then MsgWaitForMultipleObjects will return WAIT_OBJECT + n (where n is the number is event object you're wiating for).

So you probably need to handle those messages before you issue another MsgWaitForMultipleObject.

And, QS_INPUT wil react to both Mouse and Keyboard messages (>=XP Raw messages as well).

- petter

KaramChand03
March 28th, 2006, 03:46 AM
Even I am having the same problem.

Basically, I created a thread using CreateThread(...) option. Now I want to come out of MsgWait... either when the thread is finished on there is an input from the keyboard.

After u get a return from MsgWait... I need to know which handle fired the exit from the function and process accordingly.

Since I am passing only one handle - WAIT_OBJECT_0 - return value should be 0, if it came out because thread got over, but it is returning some other value.

Do you have better idea about how to handle this?

KaramChand03
March 28th, 2006, 03:51 AM
If there are messages posted to the threads message queue then MsgWaitForMultipleObjects will return WAIT_OBJECT + n (where n is the number is event object you're wiating for).

So you probably need to handle those messages before you issue another MsgWaitForMultipleObject.

And, QS_INPUT wil react to both Mouse and Keyboard messages (>=XP Raw messages as well).

- petter

So how do I only react to keyboard messages?

wildfrog
March 28th, 2006, 06:02 AM
So how do I only react to keyboard messages?
Have you tried using QS_KEY instead of QS_INPUT?

- petter

manoj.m
March 28th, 2006, 06:51 AM
Have you tried using QS_KEY instead of QS_INPUT?

- petter
Yes, i tried this also now, but the result is same.
i am pasting the code that i am using....

InvokeThread(); // invokes the thread
HANDLE h;
h = handle;//thread handle
DWORD res = MsgWaitForMultipleObjects ( 1, &h, false, INFINITE, QS_KEY );
if( ( WAIT_OBJECT_0-res ) == 0 )
Show_List();//This function uses a global structure that is filled by the thread.



--- the value that i am getting for res is always 1.

wildfrog
March 28th, 2006, 07:11 AM
Have you seen the Waiting in a message loop (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/waiting_in_a_message_loop.asp) example?

the value that i am getting for res is always 1.What do you do when it return 1? The basic idea is that you should process the message (using a normal message loop), then return back to the MsgWaitForMultipleObjects call... just as in the example.

A call to Peek/GetMessage will 'reset' the message queue so that the next call to MsgWaitForMultipleObject won't react to 'old' messages.


- petter