From: David Stuart on
Hello,

I have a thread which is looping and reading from a message queue under
Windows Mobile 5 using the INFINITE flag for the timeout.

When my app shuts down, I close the message queue and then call
WaitForSingleObject in order to wait for the thread to die.

Is this the correct approach?

My thread is waiting forever on the WaitForSingleObject call, and if I put
a breakpoint after the ReadMsgQueue in the other thread, it does not seem
to return from ReadMsgQueue even though I have called CloseMsgQueue in the
original thread.

The documentation for this API is a little strange:

dwTimeout
[in] Time in milliseconds (ms) before the read operation times out.
If set to zero, the read operation will not block if there is no data to
read. If set to INFINITE, the read operation will block until data is
available or the status of the queue changes.

What does it mean "or the status of the queue changes"?? I assumed this
meant if I closed the queue but now I am not sure.

Thanks for any help.

David
From: David Stuart on
Also just as a follow up to this, I notice that I am not calling
OpenMsgQueue, and instead am just calling CreateMsgQueue followed later
by CloseMsgQueue. Is this a problem?


On Mon, 01 May 2006 15:25:59 -0400, David Stuart wrote:

> Hello,
>
> I have a thread which is looping and reading from a message queue under
> Windows Mobile 5 using the INFINITE flag for the timeout.
>
> When my app shuts down, I close the message queue and then call
> WaitForSingleObject in order to wait for the thread to die.
>
> Is this the correct approach?
>
> My thread is waiting forever on the WaitForSingleObject call, and if I put
> a breakpoint after the ReadMsgQueue in the other thread, it does not seem
> to return from ReadMsgQueue even though I have called CloseMsgQueue in the
> original thread.
>
> The documentation for this API is a little strange:
>
> dwTimeout
> [in] Time in milliseconds (ms) before the read operation times out.
> If set to zero, the read operation will not block if there is no data to
> read. If set to INFINITE, the read operation will block until data is
> available or the status of the queue changes.
>
> What does it mean "or the status of the queue changes"?? I assumed this
> meant if I closed the queue but now I am not sure.
>
> Thanks for any help.
>
> David

From: David Stuart on
Cross-posting the question to m.p.w32.p.wince since there's (so far) no
response on this group.

Basically I am trying to figure out how to gracefully allow my thread to
die..

On Mon, 01 May 2006 15:51:42 -0400, David Stuart wrote:

> Also just as a follow up to this, I notice that I am not calling
> OpenMsgQueue, and instead am just calling CreateMsgQueue followed later
> by CloseMsgQueue. Is this a problem?
>
>
> On Mon, 01 May 2006 15:25:59 -0400, David Stuart wrote:
>
>> Hello,
>>
>> I have a thread which is looping and reading from a message queue under
>> Windows Mobile 5 using the INFINITE flag for the timeout.
>>
>> When my app shuts down, I close the message queue and then call
>> WaitForSingleObject in order to wait for the thread to die.
>>
>> Is this the correct approach?
>>
>> My thread is waiting forever on the WaitForSingleObject call, and if I put
>> a breakpoint after the ReadMsgQueue in the other thread, it does not seem
>> to return from ReadMsgQueue even though I have called CloseMsgQueue in the
>> original thread.
>>
>> The documentation for this API is a little strange:
>>
>> dwTimeout
>> [in] Time in milliseconds (ms) before the read operation times out.
>> If set to zero, the read operation will not block if there is no data to
>> read. If set to INFINITE, the read operation will block until data is
>> available or the status of the queue changes.
>>
>> What does it mean "or the status of the queue changes"?? I assumed this
>> meant if I closed the queue but now I am not sure.
>>
>> Thanks for any help.
>>
>> David

From: Ulrich Eckhardt on
David Stuart wrote:
> Cross-posting the question to m.p.w32.p.wince since there's (so far) no
> response on this group.

I only read that group, so I won't see replies in the other, just FYI.

> Basically I am trying to figure out how to gracefully allow my thread to
> die..

The neverending euthanasia discussion...

Okay, seriously:

> I have a thread which is looping and reading from a message queue under
> Windows Mobile 5 using the INFINITE flag for the timeout.
>
> When my app shuts down, I close the message queue and then call
> WaitForSingleObject in order to wait for the thread to die.
>
> Is this the correct approach?

I guess not.

> My thread is waiting forever on the WaitForSingleObject call, and if I
> put a breakpoint after the ReadMsgQueue in the other thread, it does not
> seem to return from ReadMsgQueue even though I have called CloseMsgQueue
> in the original thread.

I think it doesn't like its queue being pulled from under its feet and when
I code, I usually don't cater for such things too. So, there are two
typical solutions to this:

1. Send a 'poison value' through the queue.
The thread unblocks, sees the value, which is a signal for it to terminate,
and acts accordingly.
2. Use WaitForMultipleObjects.
The message queue is a waitable object, you use that and a normal event in a
call to WFMO and then signal the event in order to unblock and terminate
the thread.

> dwTimeout
> [in] Time in milliseconds (ms) before the read operation times out.
> If set to zero, the read operation will not block if there is no data to
> read. If set to INFINITE, the read operation will block until data is
> available or the status of the queue changes.
>
> What does it mean "or the status of the queue changes"?? I assumed this
> meant if I closed the queue but now I am not sure.

I can't tell for sure either, but I guess that it might be related to the
other side being connected or not.

Uli