Reputation:
I have three questions.
1. Is this safe to use PostThreadMessage to CWinThread?
2. Is this safe to use SendMessage(Between two applications)?
The messages can to disappear?what is the disadvantage of SendMessage,PostThreadMessage?there is a maximum count that I can to send?
3. What is the best way to kill CWinThread?
Thanks a lot!
Upvotes: 0
Views: 1624
Reputation: 51355
Is this safe to use PostThreadMessage to CWinThread?
It's safe insofar the thread posting the message won't be harmed, in case the message isn't delivered, or picked up. Messages posted to a thread get lost, when the receiving thread is in a modal loop (e.g. displaying a modal dialog, navigating through a menu, or during resize operations).
Is this safe to use SendMessage(Between two applications)?
This is generally not safe. If the target application fails to retrieve incoming messages, the sending application hangs, too. Use SendMessageTimeout or SendNotifyMessage instead.
The disadvantage of SendMessage
is, that it can lead to Message Deadlocks. The disadvantage of PostThreadMessage
is, that messages may get lost (see Why do messages posted by PostThreadMessage disappear?).
There is a maximum of 10,000 posted messages per message queue (see PostMessage).
What is the best way to kill CWinThread?
The best way to kill a CWindThread
is to not kill a CWinThread
. If you need a thread to terminate, use an Event object (or some other synchronization primitive) to signal the thread to terminate. This gives the thread a chance to perform cleanup, release global resources, and so on.
Upvotes: 2