|
-
June 1st, 2011, 07:44 AM
#1
PostThreadMessage in c#
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
-
June 1st, 2011, 09:57 AM
#2
Re: PostThreadMessage in c#
What did you mean by this:
 Originally Posted by sulabh120881
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.
Victor Nijegorodov
-
June 1st, 2011, 01:37 PM
#3
Re: PostThreadMessage in c#
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()
-
June 2nd, 2011, 01:42 PM
#4
Re: PostThreadMessage in c#
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/libr...(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
-
June 4th, 2011, 09:25 AM
#5
Re: PostThreadMessage in c#
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|