Send a message from a thread to a function with parameters
-
I've got this working: The Thread:
UINT CheckMessages(LPVOID Param) { CString tekst = "This is a tekst or maybe a structure"; ::PostMessage((HWND)Param, WM_DATARECEIVED, (WPARAM)&tekst, 0); return 0; }
The header:protected: //{{AFX_MSG(CPLCClientView) afx_msg void OnDataReceived(WPARAM wParam, LPARAM lParam); //}}AFX_MSG DECLARE_MESSAGE_MAP()
The Function:BEGIN_MESSAGE_MAP(CPLCClientView, CFormView) //{{AFX_MSG_MAP(CPLCClientView) ON_MESSAGE(WM_DATARECEIVED, OnDataReceived) //}}AFX_MSG_MAP END_MESSAGE_MAP() ... ... void CMyClass::OnDataReceived(WPARAM wParam, LPARAM lParam) { CString tekst = (CString)wParam; // or something like that.... AfxMessageBox(tekst); }
If i execute this i get a weird looking character(because i get a number with wParam), how can i send the CString towards my function? me = Visual C++ n00b -
I've got this working: The Thread:
UINT CheckMessages(LPVOID Param) { CString tekst = "This is a tekst or maybe a structure"; ::PostMessage((HWND)Param, WM_DATARECEIVED, (WPARAM)&tekst, 0); return 0; }
The header:protected: //{{AFX_MSG(CPLCClientView) afx_msg void OnDataReceived(WPARAM wParam, LPARAM lParam); //}}AFX_MSG DECLARE_MESSAGE_MAP()
The Function:BEGIN_MESSAGE_MAP(CPLCClientView, CFormView) //{{AFX_MSG_MAP(CPLCClientView) ON_MESSAGE(WM_DATARECEIVED, OnDataReceived) //}}AFX_MSG_MAP END_MESSAGE_MAP() ... ... void CMyClass::OnDataReceived(WPARAM wParam, LPARAM lParam) { CString tekst = (CString)wParam; // or something like that.... AfxMessageBox(tekst); }
If i execute this i get a weird looking character(because i get a number with wParam), how can i send the CString towards my function? me = Visual C++ n00bwillempipi wrote: void CMyClass::OnDataReceived(WPARAM wParam, LPARAM lParam) { CString tekst = (CString)wParam; // or something like that.... AfxMessageBox(tekst); } Where you cast the wParam to a CString, I think you should cast it to a CString* instead. You will find however that this too will not work as you expect though. By the time you execute the message handler, OnDataReceived, the thread proc will have already finished and the instance of a CString on the stack will no longer exist. I would suggest that you 'new' a CString in the thread proc and then 'delete' the CString in the OnDataReceived method. Chris Meech "what makes CP different is the people and sense of community, things people will only discover if they join up and join in." Christian Graus Nov 14, 2002. "AAAAAAAAAHHHHHH!!!!! Those leaks are driving me crazy! How does one finds a memory leak in a garbage collected environment ??! Daniel Turini Nov. 2, 2002.
-
willempipi wrote: void CMyClass::OnDataReceived(WPARAM wParam, LPARAM lParam) { CString tekst = (CString)wParam; // or something like that.... AfxMessageBox(tekst); } Where you cast the wParam to a CString, I think you should cast it to a CString* instead. You will find however that this too will not work as you expect though. By the time you execute the message handler, OnDataReceived, the thread proc will have already finished and the instance of a CString on the stack will no longer exist. I would suggest that you 'new' a CString in the thread proc and then 'delete' the CString in the OnDataReceived method. Chris Meech "what makes CP different is the people and sense of community, things people will only discover if they join up and join in." Christian Graus Nov 14, 2002. "AAAAAAAAAHHHHHH!!!!! Those leaks are driving me crazy! How does one finds a memory leak in a garbage collected environment ??! Daniel Turini Nov. 2, 2002.
Chris Meech wrote: I would suggest that you 'new' a CString in the thread proc can't i send the CString with the Message toward the function (like OnPaint sends a point value with it's message) so that it would look a little like this:
void CPLCClientView::OnDataReceived(CString tekst) { AfxMessageBox(tekst); }
The Visual C++ n00p -
Chris Meech wrote: I would suggest that you 'new' a CString in the thread proc can't i send the CString with the Message toward the function (like OnPaint sends a point value with it's message) so that it would look a little like this:
void CPLCClientView::OnDataReceived(CString tekst) { AfxMessageBox(tekst); }
The Visual C++ n00pwillempipi wrote: can't i send the CString with the Message toward the function (like OnPaint sends a point value with it's message) so that it would look a little like this: Since you're dealing with user defined messages, no. Sending your own message and handling it with ON_MESSAGE requires you to provide a function declarated like that you did before (with WPARAM and LPARAM as parameters). Only then the message is handled correctly. It's not comparable to messages like WM_PAINT. These are special messages handled by corresponding functions which for sure can have other parameter types. In fact, you should do it the way described before by creating your CString on the heap (calling new) and passing it as WPARAM to your function.
-
Chris Meech wrote: I would suggest that you 'new' a CString in the thread proc can't i send the CString with the Message toward the function (like OnPaint sends a point value with it's message) so that it would look a little like this:
void CPLCClientView::OnDataReceived(CString tekst) { AfxMessageBox(tekst); }
The Visual C++ n00pNo you can not pass parameters for user defined messages in that way. I would suggest that you thoroughly read this article[^]. It is one of the best on this site explaining multi-threading and how to deal with the many issues. Chris Meech "what makes CP different is the people and sense of community, things people will only discover if they join up and join in." Christian Graus Nov 14, 2002. "AAAAAAAAAHHHHHH!!!!! Those leaks are driving me crazy! How does one finds a memory leak in a garbage collected environment ??! Daniel Turini Nov. 2, 2002.
-
willempipi wrote: can't i send the CString with the Message toward the function (like OnPaint sends a point value with it's message) so that it would look a little like this: Since you're dealing with user defined messages, no. Sending your own message and handling it with ON_MESSAGE requires you to provide a function declarated like that you did before (with WPARAM and LPARAM as parameters). Only then the message is handled correctly. It's not comparable to messages like WM_PAINT. These are special messages handled by corresponding functions which for sure can have other parameter types. In fact, you should do it the way described before by creating your CString on the heap (calling new) and passing it as WPARAM to your function.
Schlaubi wrote: creating your CString on the heap (calling new) how do you mean this precisly? like this: CString tekst = new CString; and, after i created the tekst, do i have to delete it?
-
Schlaubi wrote: creating your CString on the heap (calling new) how do you mean this precisly? like this: CString tekst = new CString; and, after i created the tekst, do i have to delete it?
-
in your thread:
CString *tekst = new CString("blablabla"); PostMessage(hWnd, WM_MYMESSAGE, (WPARAM)tekst, 0);
in MyFunc: ...CString* pStr = (CString*)wParam; do_sth_with_string delete pStr;
Thnx...:-D:-D:-D