Click to See Complete Forum and Search --> : How do I know that a message has been processed?


Andrew Truckle
April 12th, 1999, 01:06 AM
In a previous query, someone gave me the following idea:
******************************
In the thread you can send the message using something like:

CString sendstring = _T("Send this text");
char *lpszPostedBytes = (char *) LocalAlloc(LPTR,sendstring.Length() + 1);
if (lpszPostedBytes != NULL)
{
strcpy(lpszPostedBytes, (LPCTSTR) sendstring);
PostMessage(myWnd, YOUR_MESSAGE, (LPARAM) lpszPostedBytes, 0L);
}

Then release the memory when the message been processed using:
LocalFree();
******************************
I've implemented this with only one problem. I don't know where, or when to release the memory using LocalFree().

If I do it after calling ::PostMessage(), it frees the memory before processing the message. So what do I do!!!!

Emil
April 12th, 1999, 01:45 AM
Using SendMessage (instead of PostMessage) would help you. A call to SendMessage returns after the message has benn processed.

Otherwise you could always let the receiver of the message free the allocated bytes... But using SendMessage seams more appropiate.


------------------------------
Emil Gustafsson, emil@ntier.se
NTier Solutions AB

Andrew Truckle
April 12th, 1999, 01:49 AM
Hi, I will try what you said. Only, someone suggested this earlier, but another person gave this reply:

check out:
http://www.codeguru.net/bbs/wt/showpost.pl?Board=vc&Number=2579&page=0&view=collapsed&sb=5#Post2579

Tell me what you think.

Andrew Truckle
April 12th, 1999, 02:15 AM
Hi again,

I've just tried SendMessage and all seems great to me - cheers!

Emil
April 12th, 1999, 03:38 AM
Well, SendMessage is always a hazard if your code is written so that deadlocks might happen.

As I see it, to deadlock, when sending messages to the main app, there must be some "SendMessage" from the main app to the thread. i.e. to deadlock, two threads must send each other messages. If this is the case, you better let the receiver free data sent to them so that you may use PostMessage.

And, if you have threads sending messages to each other (and hence a risk for dead-locks) and don't want to let the receiver free the data, the problem (of deadlocks with SendMessage) can always be solved using critical sections...

------------------------------
Emil Gustafsson, emil@ntier.se
NTier Solutions AB

Andrew Truckle
April 12th, 1999, 03:44 AM
Hi again

Thanks for the advice.

To be honest, my application is not that complicated. It begins another thread which sends messages back to the calling thread and not the other way, so I don't think a deadlock will happen.

Emil
April 12th, 1999, 03:57 AM
Neither can I.
With one way communication there just can't be any deadlocks...


------------------------------
Emil Gustafsson, emil@ntier.se
NTier Solutions AB