views:

100

answers:

2

This is closely related to an earlier question.

In the managed world:

  • How do I check if the current thread has messages in its message queue?
  • How do I yield to the OS and wait for a message in the current thread (like GetMessage or WaitMessage)?

I am looking for the managed equivalents without PInvoke.

+3  A: 

You could override WndProc and then fire your own event when it gets called. This, of course, wouldn't be blocking. If you want to block until a message, you could have a method in another thread waiting on a synchronisation object and have the WndProc signal that object.

Jeff Yates
This looks like the best bet I totally missed the question thought you were looking to pump the message queue;-)
JoshBerke
Which WndProc would you override? Every control has its own.
Hans Passant
A: 

You can use the AddMessageFilter function.

I don't think there's a blocking way how to do that without P/Invoke, since it's beyond intentions of .NET.

arul
Why should waiting for a message violate the design principles of .NET? Just curious. In what way is it harmful?
Vulcan Eager
Not exactly violate, you can still do it, that's why there's stuff like P/Invoke. Maybe I should have written Windows Forms, but the point remains unchanged.
arul
Just wanted to point out that AddMessageFilter does not "check if the current thread has messages" nor does it "wait for a message in the current thread" as required by my post.
Vulcan Eager
However, I must accept this as the answer since it's the one that gets closest to what I want to do.
Vulcan Eager