Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
struct ThreadParam
{
CMyDialog* mDlg;
};
UINT MyThreadProc( LPVOID pParam )
{
ThreadParam* p = static_cast<ThreadParam*> (pParam);
p->mDlg->callSomeMethod();
}
void CMyDialog::OnSomeButton()
{
...
ThreadParam param;
param.mDlg = this;
AfxBeginThread(MyThreadProc, ¶m);
...
}
// Defining a 'user defined' message for the call I wish to make
#define MYMESS_CALL_SOME_METHOD (WM_APP + 1)
struct ThreadParam
{
HWND mDlg; // Note: A handle.
};
UINT MyThreadProc( LPVOID pParam )
{
ThreadParam* p = static_cast<ThreadParam*> (pParam);
// Using message to call the method. Note: I could of course use the
// WPARAM, LPARAM and returned value for something meaningful if I wished.
::SendMessage(p->mDlg, MYMESS_CALL_SOME_METHOD, 0, 0);
delete p;
}
void CMyDialog::OnSomeButton()
{
...
ThreadParam* param = new ThreadParam;
param->mDlg = m_hWnd; // A handle, not a dangerous 'this'
AfxBeginThread(MyThreadProc, param);
param = 0; // The other thread shall delete it
...
}
//{{AFX_MSG(CMyDialog)
...
afx_msg LRESULT OnCallSomeMethod(WPARAM, LPARAM);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
BEGIN_MESSAGE_MAP(CMyDialog, CDialog)
//{{AFX_MSG_MAP(CMyDialog)
...
ON_MESSAGE(MYMESS_CALL_SOME_METHOD, OnCallSomeMethod)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
LRESULT CMyDialog::OnCallSomeMethod(WPARAM wp, LPARAM lp)
{
callSomeMethod();
return 0;
}
CString a ="Foo";
CString b = a;
CString c(a);
struct ThreadParam
{
CString mSomeString;
};
...
CString foo="Foo";
ThreadParam* param = new ThreadParam;
// Looks like a copy, but internally
// param->mSomeString points to foo's internals:
param->mSomeString = foo;
AfxBeginThread(MyThreadProc, param);
// Making sure mSomeString doesn't
// 'see' the CString it's copied from
param->mSomeString = static_cast<LPCTSTR>(foo);