A smart pointer is a class that wraps a "bare" C++ pointer, to manage the lifetime of the object being pointed to.
With "bare" C++ pointers, the programmer has to explicitly destroy the object when it is no longer useful.
#define MY_WM_MESSAGE1 (WM_APP + 1)
Your secondary thread will need an hwnd (not a CWnd*) to post to. You can pass the main hwnd when you create the thread. Then you are ready to send or post a message from the secondary thread:
::PostMessage(hwnd, MY_WM_MESSAGE1, (WPARAM)0, (LPARAM)0);
or,
::SendMessage(hwnd, MY_WM_MESSAGE1, (WPARAM)0, (LPARAM)0);
The lParam and wParam can be used to pass any parameters you like to the destination. PostMessage puts the message in the main thread's queue and returns quickly. The main thread processes it asynchronously. SendMessage does not return until the main thread actually gets and processes the message, so it provides interthread synchronization.
afx_msg LRESULT OnMyMessage1(UINT wParam, LONG lParam);
The next step is to enter your message handler into the message map, typing it in manually instead of using the wizard. Put in an ON_MESSAGE macro like the following after the "//}}AFX_MSG_MAP" line and before the "END_MESSAGE_MAP()" line.
//}}AFX_MSG_MAP
ON_MESSAGE(MY_WM_MESSAGE1, OnMyMessage1)
END_MESSAGE_MAP()
This adds your message and function into the message mapping table. When the message comes in to the main window your function will be called:
LRESULT CMainFrame::OnMyMessage1(UINT wParam, LONG lParam)
{
... do something about the message from secondary thread
return 0; // I handled this message
}
Copyright ©2010 Visual Cpp Questions