-
October 20th, 2022, 11:52 PM
#1
postmessage
Hi All,
sorry for asking newbie questions. trying to learn the concept: Calling PostMessage from inside a thread function which could be member of a dialog class. When to omit the first parameter?. I see examples of both.
::PostMessage( dlghandle , WM_SOMEMSG , (WPARAM)&s, 0 );
::PostMessage( WM_SOMEMSG , (WPARAM)&s, 0 );
also, how to use PostMessage with WaitForSingleObject , events or whatever else to get notified after the message in queue is processed.
-
October 21st, 2022, 01:44 AM
#2
Re: postmessage
Originally Posted by @EE@
Hi All,
sorry for asking newbie questions. trying to learn the concept: Calling PostMessage from inside a thread function which could be member of a dialog class. When to omit the first parameter?. I see examples of both.
::PostMessage( dlghandle , WM_SOMEMSG , (WPARAM)&s, 0 );
::PostMessage( WM_SOMEMSG , (WPARAM)&s, 0 );
also, how to use PostMessage with WaitForSingleObject , events or whatever else to get notified after the message in queue is processed.
This one:
Code:
::PostMessage( dlghandle , WM_SOMEMSG , (WPARAM)&s, 0 );
is correct presuming the dlghandle is valid HWND.
Ths one:
Code:
::PostMessage( WM_SOMEMSG , (WPARAM)&s, 0 );
is wrong cause there is no such a function in a global scope ( :: means global scope).
Also have a look at http://flounder.com/workerthreads.htm
Victor Nijegorodov
-
October 22nd, 2022, 10:24 AM
#3
Re: postmessage
what is difference between these two?:
::PostMessage( dlghandle , WM_SOMEMSG , (WPARAM)&s, 0 );
PostMessage( WM_SOMEMSG , (WPARAM)&s, 0 );
-
October 22nd, 2022, 11:11 AM
#4
Re: postmessage
This was answered by Victor above.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 23rd, 2022, 04:40 AM
#5
Re: postmessage
Originally Posted by @EE@
what is difference between these two?:
::PostMessage( dlghandle , WM_SOMEMSG , (WPARAM)&s, 0 );
PostMessage( WM_SOMEMSG , (WPARAM)&s, 0 );
The first one is a Windows API function.
The second one looks like a method of MFC CWnd class
Victor Nijegorodov
-
October 23rd, 2022, 07:03 PM
#6
Re: postmessage
victor and 2Kaud, thank you for very kind help. I learned a lot.
-
October 27th, 2022, 02:00 AM
#7
Re: postmessage
Taking a look in your sample code...
Code:
::PostMessage( dlghandle , WM_SOMEMSG , (WPARAM)&s, 0 );
PostMessage( WM_SOMEMSG , (WPARAM)&s, 0 );
...I have to add a little but important note, beside what Victor already answered. It looks like you are passing in WPARAM a pointer to an object (&s). This must be usually avoided, because unlike SendMessage function, PostMessage is asynchronous, i.e. it simply places the message in the message queue, then returns immediately. Until the message is processed, that object may be destroyed.
Here is an example:
Code:
void CDemoDialog::OnSomeButtonClicked()
{
// ...
CShape shape;
PostMessage(WM_SOMEMSG, (WPARAM)&shape);
//...
// ...
} // Here, 'shape' object goes out-of-scope and is being destroyed.
// WM_SOMEMSG is not yet processed and further leads in troubles.
In this case prefer using SendMessage.
Last edited by ovidiucucu; October 27th, 2022 at 02:04 AM.
-
October 27th, 2022, 01:15 PM
#8
Re: postmessage
Well I agree with Ovidiu about the danger in passing in WPARAM/LPARAM a pointer to an object (&s).
However, there may be a following workaround (suggested some decades back by Joe Newcomer):
- create a new object in the heap
- post this address as WPARAM/LPARAM in the PostMessage
- the receiver (it is in the most case the main thread) is now responsible for proper deleting the received object
Victor Nijegorodov
-
October 28th, 2022, 02:28 AM
#9
Re: postmessage
Thank you, Victor!
Just to add a method in which the receiver doesn't need to delete: using of smart pointers, i.e. std::unique_ptr since C++11.
Here is an example:
Code:
void CTestDlg::OnSomeButtonClicked()
{
auto spShape = std::make_unique<CShape>();
// ...
LPARAM lParam = reinterpret_cast<LPARAM>(spShape.get());
PostMessage(WM_SOMEMSG, 0, lParam);
spShape.release(); // release the pointer owmership
}
LRESULT CTestDlg::OnSomeMsg(WPARAM wParam, LPARAM lParam)
{
std::unique_ptr<CShape> spShape(reinterpret_cast<CShape*>(lParam));
spShape->DoSomething();
// ...
// The receiver doesn't need to delete;
// it is automatically done when spShape goes out-of-scope.
return 0;
}
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
|