Interthread communication and services

Hi,

I am currently writing a windows service (target platform is Win2K), that
utilizes a number of
thread. I was going to use message queues for interthread communication
(i.e. by using
PostThreadMessage and MsgWaitForMulitpleObject). However, any call to
PostThreadMessage returns
with a value of `ERROR_INVALID_THREAD_ID’.

From the documentation, this error code can be returned in three cases:

  1. if the thread id is not a valid thread id
  2. if the thread does not have a message queue
  3. if the thread does not belong to the same desktop as the calling thread
    or to a
    process with the same LUID. (windows 2000 / XP only)

I believe that I am passing a valid thread id, and the called thread does
have a message queue
(I call PeekMessage at the start of the thread to create the message queue.
However, PeekMessage
returns true if there are messages to retrieve, false otherwise – so the
message queue may not
be set up.)

The third case concerns me. Since a service typically does not run with a
desktop, I suspect that since the service does not have an associated
desktop,
that the message queues are never being set up.

Based on the above, I have three questions:

  1. Can anyone confirm my suspicion that the message queues are not begin
    setup.

  2. If my suspicion is wrong, does anyone have any other thoughts why
    PostThreadMessage is
    failing.

  3. How would one do interthread communication, especially when data needs to
    be passed between
    the threads. Do we need to resort to global/shared variables and events
    to signal when there
    is data to be read.

Thanks,
George Huber
SRI, International

Did you try to use APC (QueueUserAPC() ) ? I think it’s the better solution for
non GUI programs.
In waing threads, you need to use Ex functions that are alertables, like
SleepEx(TRUE);

Hope this help

Sylvain

“Huber, George K CECOM RDEC STCD SRI” a ?crit :

Hi,

I am currently writing a windows service (target platform is Win2K), that
utilizes a number of
thread. I was going to use message queues for interthread communication
(i.e. by using
PostThreadMessage and MsgWaitForMulitpleObject). However, any call to
PostThreadMessage returns
with a value of `ERROR_INVALID_THREAD_ID’.

>From the documentation, this error code can be returned in three cases:

  1. if the thread id is not a valid thread id
  2. if the thread does not have a message queue
  3. if the thread does not belong to the same desktop as the calling thread
    or to a
    process with the same LUID. (windows 2000 / XP only)

I believe that I am passing a valid thread id, and the called thread does
have a message queue
(I call PeekMessage at the start of the thread to create the message queue.
However, PeekMessage
returns true if there are messages to retrieve, false otherwise – so the
message queue may not
be set up.)

The third case concerns me. Since a service typically does not run with a
desktop, I suspect that since the service does not have an associated
desktop,
that the message queues are never being set up.

Based on the above, I have three questions:

  1. Can anyone confirm my suspicion that the message queues are not begin
    setup.

  2. If my suspicion is wrong, does anyone have any other thoughts why
    PostThreadMessage is
    failing.

  3. How would one do interthread communication, especially when data needs to
    be passed between
    the threads. Do we need to resort to global/shared variables and events
    to signal when there
    is data to be read.

Thanks,
George Huber
SRI, International


You are currently subscribed to ntdev as: xxxxx@libertysurf.fr
To unsubscribe send a blank email to xxxxx@lists.osr.com

Try using COM instead. It will give you a lot of existing framework
code and simplify your task.

Max

----- Original Message -----
From: “Huber, George K CECOM RDEC STCD SRI”
<george.k.huber>
To: “NT Developers Interest List”
Sent: Thursday, February 27, 2003 4:33 PM
Subject: [ntdev] Interthread communication and services

> Hi,
>
> I am currently writing a windows service (target platform is Win2K),
that
> utilizes a number of
> thread. I was going to use message queues for interthread
communication
> (i.e. by using
> PostThreadMessage and MsgWaitForMulitpleObject). However, any call
to
> PostThreadMessage returns
> with a value of `ERROR_INVALID_THREAD_ID’.
>
> From the documentation, this error code can be returned in three
cases:
>
> 1. if the thread id is not a valid thread id
> 2. if the thread does not have a message queue
> 3. if the thread does not belong to the same desktop as the calling
thread
> or to a
> process with the same LUID. (windows 2000 / XP only)
>
> I believe that I am passing a valid thread id, and the called thread
does
> have a message queue
> (I call PeekMessage at the start of the thread to create the message
queue.
> However, PeekMessage
> returns true if there are messages to retrieve, false otherwise –
so the
> message queue may not
> be set up.)
>
> The third case concerns me. Since a service typically does not run
with a
> desktop, I suspect that since the service does not have an
associated
> desktop,
> that the message queues are never being set up.
>
> Based on the above, I have three questions:
>
> 1) Can anyone confirm my suspicion that the message queues are not
begin
> setup.
>
> 2) If my suspicion is wrong, does anyone have any other thoughts why
> PostThreadMessage is
> failing.
>
> 3) How would one do interthread communication, especially when data
needs to
> be passed between
> the threads. Do we need to resort to global/shared variables and
events
> to signal when there
> is data to be read.
>
> Thanks,
> George Huber
> SRI, International
>
>
> —
> You are currently subscribed to ntdev as: xxxxx@storagecraft.com
> To unsubscribe send a blank email to
xxxxx@lists.osr.com
></george.k.huber>