PostThreadMessage equivalent

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Please, help.

Is there a PostThreadMessage equiavalent functionality in .NET framework?

I have messages coming from a COM server and they are being delivered in
different threads. I would like to receive these messages in one thread,
thus synchronize them. I don't want to use form's Invoke method, but
something similar, thread-related.

Thanks for any suggestions.

VR
 
The BeginInvoke method (a form has it) is *the* recommended way in .NET
framework to transfer control back to the primary thread.
It can be used with an appropriate delegate to call any method on the form,
and as long as it is the only way a method in another thread calls a form
method, then it is safe to keep a reference to the form in any thread in
order to invoke the delegate, as you have to specify what object to invoke it
 
Patty,

Thanks for your post. I do realize that using forms is the preferred way.

The problem is that the application in question is not a windows GUI
application. I don't have a form in it, but I do have a primary thread which
needs to receive messages.

Is there any way one thread could execute a delegate in the context of a
particular different thread (given that it's not a form)?

Thanks,
VR
 
But if there isn't a UI thread (or a thread with a message pump/queue ) you
can't use PostThreadMessage either. Also I'm not clear why the COM server
delivers the messages to different threads.

Anyway, one option would be to enque the messages and let the receiving
thread dequeue the messages, taking care to synchronise the access to the
Queue.

Willy.
 
I don't mind it to be a thread with a message pump/queue, as long as it's
not a UI thread. I'd like to know, though, whether .NET framework has an
implementation for it (threads with a pump/queue). I guess I could
implenment the queue myself, and use events to signal to thread, but I was
just wondering if threre is already something like this exists.

Thanks,
VR
 
This can be done using some PInvoke interop, basically you need to:

1 - spawn a new thread as background thread using the ThreadStart delegate
2 - in the thread procedure you call Win32 GetCurrentThreadId and save it in
a class variable so it can be accessed by your COM receiving threads.

[DllImport("kernel32"), SuppressUnmanagedCodeSecurityAttribute]
static extern int GetCurrentThreadId();

3 - then you need to call Win32 GetMessage to retrieve the message from the
message queue, with the hwnd argument set to 0 (IntPtr.Zero).

[DllImport("user32"), SuppressUnmanagedCodeSecurityAttribute]
public static extern int GetMessage(ref MSG msg, IntPtr hwnd , ushort
fMin, uint fMax);

The received messages can be retrieved from following MSG structure.
public struct MSG{
public IntPtr hwnd;
public uint message;
public uint wParam;
public uint lParam;
public uint time;
public int x;
public int y;
}
4 - the calling threads, have to call Win32 PostThreadMessage using the
thread ID stored in 2.

[DllImport("user32"), SuppressUnmanagedCodeSecurityAttribute]
public static extern bool PostThreadMessage(int threadId, uint msg ,
ushort wParam, uint lParam);

Willy.
 
Willy,

Thanks for your help. That's what I was looking for.

VR

Willy Denoyette said:
This can be done using some PInvoke interop, basically you need to:

1 - spawn a new thread as background thread using the ThreadStart delegate
2 - in the thread procedure you call Win32 GetCurrentThreadId and save it in
a class variable so it can be accessed by your COM receiving threads.

[DllImport("kernel32"), SuppressUnmanagedCodeSecurityAttribute]
static extern int GetCurrentThreadId();

3 - then you need to call Win32 GetMessage to retrieve the message from the
message queue, with the hwnd argument set to 0 (IntPtr.Zero).

[DllImport("user32"), SuppressUnmanagedCodeSecurityAttribute]
public static extern int GetMessage(ref MSG msg, IntPtr hwnd , ushort
fMin, uint fMax);

The received messages can be retrieved from following MSG structure.
public struct MSG{
public IntPtr hwnd;
public uint message;
public uint wParam;
public uint lParam;
public uint time;
public int x;
public int y;
}
4 - the calling threads, have to call Win32 PostThreadMessage using the
thread ID stored in 2.

[DllImport("user32"), SuppressUnmanagedCodeSecurityAttribute]
public static extern bool PostThreadMessage(int threadId, uint msg ,
ushort wParam, uint lParam);

Willy.


I don't mind it to be a thread with a message pump/queue, as long as it's
not a UI thread. I'd like to know, though, whether .NET framework has an
implementation for it (threads with a pump/queue). I guess I could
implenment the queue myself, and use events to signal to thread, but I was
just wondering if threre is already something like this exists.

Thanks,
VR

thread
in want
to
 
Back
Top