Replacement for postmessage

A

Alexander Muylaert

Hi

I need a replace for postmessage.

Sometimes I would like to notify the Application thread of some action. For
example, one thread is reading buffers from a socket, pushing them on a
queue list and I need to notify the main thread that he can start poping
buffers from this queue.

in Win32 I would just use a simple PostMessage where Msg > WM_USER. How
can I do the same in .Net. I should work on the CF to.

kind regards

Alexander
 
A

Alexander Muylaert

Correct me if I'm wrong

I would implement a private Control inside my class and use the BeginInvoke
method to do my desired behaviour.
Do I need to call the EndInvoke method or can I just void the result
BeginInvoke returns?

Isn't their a better way, this seems ugly complex for a simple thing.

kind regards

Alexander
 
R

Richard Blewett [DevelopMentor]

The best thing to use for this Producer/Consumer pattern would be to use the Monitor Wait and Pulse methods. The thread that should read issues a Wait and the producer thread (putting things on the queue issues a Pulse when something is available. Then the Waiting thread wakes up and reads the queue. Unfortunately these methods aren't available on the CF. So in that case, assuming your application thread is a windows forms based application, use the Control.Invoke method to marshal a call to the UI thread for it to read the queue.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

Hi

I need a replace for postmessage.

Sometimes I would like to notify the Application thread of some action. For
example, one thread is reading buffers from a socket, pushing them on a
queue list and I need to notify the main thread that he can start poping
buffers from this queue.

in Win32 I would just use a simple PostMessage where Msg > WM_USER. How
can I do the same in .Net. I should work on the CF to.

kind regards

Alexander
 
A

Alexander Muylaert

Hi,

thanks for your reply

The problem is that I don't want my main thread to block. Therefor Wait is
not an option for me.

Postmessage was perfect an I need something equally simple. :)

Kind regards

Alexander
 
R

Robert Jordan

Alexander said:
Correct me if I'm wrong

I would implement a private Control inside my class and use the BeginInvoke
method to do my desired behaviour.

The control must be visible (it must have a HWND assigned)
to be able to do that, AFAIK.

If you want to use the *same* pattern like in your old apps
(PostMessage), then you have to handle the async calls
from within your forms (that already support Invoke/BeginInvoke).
Do I need to call the EndInvoke method or can I just void the result
BeginInvoke returns?

Isn't their a better way, this seems ugly complex for a simple thing.

See Richard's answer. The "Consumer" may not be your UI thread.
It can be another thread that communicates with the UI using
Invoke/BeginInvoke.

bye
Rob
 
R

Richard Blewett [DevelopMentor]

Thats also why I suggested Control.Invoke (which is CF friendly). This assumes your application thread is pumping messages of course (as does PostMessage) so the thread would have had to call Application.Run

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

Hi,

thanks for your reply

The problem is that I don't want my main thread to block. Therefor Wait is
not an option for me.

Postmessage was perfect an I need something equally simple. :)

Kind regards

Alexander
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top