CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2006
    Posts
    221

    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

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: PostThreadMessage in c#

    What did you mean by this:
    Quote Originally Posted by sulabh120881 View Post
    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

  3. #3
    Join Date
    Nov 2006
    Posts
    221

    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()

  4. #4
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    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

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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
  •  





Click Here to Expand Forum to Full Width

Featured