Click to See Complete Forum and Search --> : PostThreadMessage in c#


sulabh120881
June 1st, 2011, 07:44 AM
Hi,


I am using PostThreadMessage() for sending data from my main form to a thread.
and receiving with GetMessage()


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

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


if I send msg in interger format then I am able to get the data.


But I want to send/recv data in structure format only.
Can anybody tell me how this can be done in c#


Thanks in advance

VictorN
June 1st, 2011, 09:57 AM
What did you mean by this:if I send msg in interger format then I am able to get the data.

But I want to send/recv data in structure format only.
:confused:

sulabh120881
June 1st, 2011, 01:37 PM
Hi,


Here I want to say

in PostThreadMessage(int threadId, uint msg, ushort wParam, uint lParam);

if I send msg = 10, lParam = 20, I can see the data in GetMessage's MSG structure.


But if I want to send some string value how I can send it.


I tried it

string s = "bob";
char[] buff = new char[100];
s.CopyTo(0, buff, 0, s.Length);

unsafe
{
fixed (char* pbuff = buff) postit((uint)pbuff);
}

where postit is

PostThreadMessage(idthread, 11, 0, buff);


but not able to recv this string with GetMessage()

MikeAThon
June 2nd, 2011, 01:42 PM
Are you using PostThreadMessage across different processes, or merely between threads of the same process? If you are posting across different processes, then since each process has its own memory, one process will not be able to see the memory content of another.

You might want to consider the WM_COPYDATA message if posting between different processes: http://msdn.microsoft.com/en-us/library/ms649011(v=vs.85).aspx

Incidentally, use of PostThreadMessage is definitely BAD if the target thread has any visible UI. If the UI of the target thread is being manipulated by the user, then the posted message will be LOST: "PRB: PostThreadMessage Messages Lost When Posted to UI Thread" at http://support.microsoft.com/kb/183116

Mike

Arjay
June 4th, 2011, 09:25 AM
In C#, there's no need to call postthreadmessage. Instead use the builtin threading functionality of C#.

Like backgroundworker class. Also check out InvokeRequired property.