PostMessage - Page 2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 25 of 25

Thread: PostMessage

  1. #16
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,466

    Re: PostMessage

    Quote Originally Posted by MasterDucky View Post
    No, I havent. I use codeblocks with MSVC 2010 and I don't know how to debug with it.
    Then you must first learn how to debug with MSVC 2010 when using codeblocks! In VS itself it is prettu easy: set a breakpoint (F9) on a line of your code, then press F5 to start debugger, and after it breaks on your breakpoint perform the next steps using F10... Don"t forget to look at the Autos/Watch windows...

    BTW, is your build a UNICODE one or ANSI?
    Victor Nijegorodov

  2. #17
    Join Date
    Dec 2007
    Location
    France
    Posts
    229

    Re: PostMessage

    It's ANSI.

    Yes I definitely need to learn to debug first.

  3. #18
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,466

    Re: PostMessage

    Quote Originally Posted by MasterDucky View Post
    It's ANSI.

    Yes I definitely need to learn to debug first.
    Well, I'd rewrite your code to make a bit more simple:
    Code:
        while (getline(ss, str, '?'))
        {
            string* pItem = new string(str);
            if(!PostMessage(hwnd, WM_USERDEFINED, (WPARAM)pItem, 0))
                  delete pItem;
        }
    Code:
        case WM_USERDEFINED:
        {
            string* pItem = (string*)wParam;
            SendMessage(hListBox, LB_ADDSTRING, (WPARAM)0, (LPARAM)pItem->c_str());
            delete pItem;
        }
    Victor Nijegorodov

  4. #19
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,962

    Re: PostMessage

    Quote Originally Posted by MasterDucky View Post
    Code:
     
        string str;
        stringstream ss(s);
    
        while (getline(ss, str, '?'))
        {
            TCHAR* pItem = new TCHAR[MAX_PATH];
            lstrcpy(pItem, (TCHAR*)str.c_str());
            if(!PostMessage(hwnd, WM_USERDEFINED, (WPARAM)pItem, 0))
             delete pItem;
        }
    should be a delete [] pItem; but that don't really make a difference for this code.

  5. #20
    Join Date
    Dec 2007
    Location
    France
    Posts
    229

    Re: PostMessage

    Mea culpa, I forgot a 'delete pItem;' at the end of another thread, that was it!

  6. #21
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,466

    Re: PostMessage

    Quote Originally Posted by MasterDucky View Post
    Mea culpa, I forgot a 'delete pItem;' at the end of another thread, that was it!
    Well, therefore I asked you to post your actual code, not the one you created a week or so ago!
    Victor Nijegorodov

  7. #22
    Join Date
    Dec 2007
    Location
    France
    Posts
    229

    Re: PostMessage

    Quote Originally Posted by VictorN View Post
    Well, I'd rewrite your code to make a bit more simple:
    Code:
        while (getline(ss, str, '?'))
        {
            string* pItem = new string(str);
            if(!PostMessage(hwnd, WM_USERDEFINED, (WPARAM)pItem, 0))
                  delete pItem;
        }
    Code:
        case WM_USERDEFINED:
        {
            string* pItem = (string*)wParam;
            SendMessage(hListBox, LB_ADDSTRING, (WPARAM)0, (LPARAM)pItem->c_str());
            delete pItem;
        }
    Thanks, that's a good tip!

    Interesting that PostMessage can take a string* directly but SendMessage cannot.
    Last edited by MasterDucky; July 1st, 2014 at 08:07 AM.

  8. #23
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,357

    Re: PostMessage

    MasterDucky, another approach is to use a critical section and share the string (I believe ORuebens has mentioned this or a similar approach earlier).

    The way I approach passing data between threads is to share the data between the threads using a critical section to synchronize access to the data. When 'sending' data from the worker thread to the UI thread, I only use PostMessage to signal the UI thread there is data to read. In the message handler, the UI thread locks the cs, reads the data and then unlocks the cs.

    I use RAII for the synchronization and the whole thing ends up being very clean. In fact if you can encapsulate the thread synchronization inside a thread and provide thread safe get and set methods all the better. Using this approach, you would create an instance of this class in the UI thread and then pass a pointer to this class to the worker thread. In the worker thread, you call the set method, PostMessage (to signal the UI thread). In the UI thread's message handler, you simply call the get method to retrieve the data. The locking/unlock would all be encapsulated within the class, so the users of this class need not worry about threading specifics.

    I have a few threading related articles in the link in my signature line. Check out the one called SimpleThread.

  9. #24
    Join Date
    Dec 2007
    Location
    France
    Posts
    229

    Re: PostMessage

    It's a pity it's in MFC but Im gonna check it out nevertheless, thank you Arjay.

  10. #25
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,357

    Re: PostMessage

    Quote Originally Posted by MasterDucky View Post
    It's a pity it's in MFC but Im gonna check it out nevertheless, thank you Arjay.
    Yes the UI portions of the samples are, but the threading and lock implementations don't use anything MFC.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center