-
July 1st, 2014, 07:03 AM
#16
Re: PostMessage
Originally Posted by MasterDucky
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
-
July 1st, 2014, 07:15 AM
#17
Re: PostMessage
It's ANSI.
Yes I definitely need to learn to debug first.
-
July 1st, 2014, 07:26 AM
#18
Re: PostMessage
Originally Posted by MasterDucky
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
-
July 1st, 2014, 07:36 AM
#19
Re: PostMessage
Originally Posted by MasterDucky
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.
-
July 1st, 2014, 07:37 AM
#20
Re: PostMessage
Mea culpa, I forgot a 'delete pItem;' at the end of another thread, that was it!
-
July 1st, 2014, 07:42 AM
#21
-
July 1st, 2014, 07:59 AM
#22
Re: PostMessage
Originally Posted by VictorN
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.
-
July 1st, 2014, 09:05 AM
#23
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.
-
July 1st, 2014, 02:20 PM
#24
Re: PostMessage
It's a pity it's in MFC but Im gonna check it out nevertheless, thank you Arjay.
-
July 1st, 2014, 03:06 PM
#25
Re: PostMessage
Originally Posted by MasterDucky
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.
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
|