Click here to Skip to main content
15,887,338 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: And he would be violating the rules of threading. Pin
Emilio Garavaglia25-Jun-10 3:04
Emilio Garavaglia25-Jun-10 3:04 
QuestionHow could I route all the windows messages of a dialog to a thread with their parameters? Pin
m_code2-May-10 8:43
m_code2-May-10 8:43 
AnswerRe: How could I route all the windows messages of a dialog to a thread with their parameters? Pin
Richard Andrew x642-May-10 8:53
professionalRichard Andrew x642-May-10 8:53 
AnswerRe: How could I route all the windows messages of a dialog to a thread with their parameters? Pin
Michel Godfroid2-May-10 8:58
Michel Godfroid2-May-10 8:58 
GeneralRe: How could I route all the windows messages of a dialog to a thread with their parameters? Pin
m_code2-May-10 9:07
m_code2-May-10 9:07 
GeneralRe: How could I route all the windows messages of a dialog to a thread with their parameters? Pin
Michel Godfroid2-May-10 9:22
Michel Godfroid2-May-10 9:22 
GeneralRe: How could I route all the windows messages of a dialog to a thread with their parameters? Pin
m_code2-May-10 9:57
m_code2-May-10 9:57 
GeneralRe: Sample code, the good way Pin
Software_Developer2-May-10 19:08
Software_Developer2-May-10 19:08 
The API functions ::PostMessage and ::SendMessage are thread safe!
That means you can use the windows handle (ie the CWnd::m_hWnd) to interact with your MFC classes.

// 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    ...
}


Declare a method in the dialog message map (in the .h file)

//{{AFX_MSG(CMyDialog)
    ...
    afx_msg LRESULT OnCallSomeMethod(WPARAM, LPARAM);
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()

Map the message to the method and implement the method (in the .cpp file):

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;
}

GeneralRe: Sample code, the good way Pin
Randor 3-May-10 6:22
professional Randor 3-May-10 6:22 
GeneralRe: SendMessage is thread-safe because no interaction between the threads Pin
Software_Developer3-May-10 20:01
Software_Developer3-May-10 20:01 
GeneralRe: SendMessage is thread-safe because no interaction between the threads Pin
Randor 4-May-10 5:55
professional Randor 4-May-10 5:55 
GeneralRe: Link to an example (code) Pin
Software_Developer2-May-10 19:41
Software_Developer2-May-10 19:41 
GeneralRe: Link to an example (code) Pin
m_code4-May-10 7:28
m_code4-May-10 7:28 
QuestionWhat does my Logitech keyboard send when I press its special 'E-Mail' key? Pin
glyfyx2-May-10 7:43
glyfyx2-May-10 7:43 
AnswerRe: What does my Logitech keyboard send when I press its special 'E-Mail' key? Pin
Richard Andrew x642-May-10 8:30
professionalRichard Andrew x642-May-10 8:30 
AnswerRe: What does my Logitech keyboard send when I press its special 'E-Mail' key? Pin
Michel Godfroid2-May-10 9:44
Michel Godfroid2-May-10 9:44 
AnswerRe: What does my Logitech keyboard send when I press its special 'E-Mail' key? Pin
glyfyx2-May-10 18:01
glyfyx2-May-10 18:01 
QuestionPointer/address to CFormView Pin
DanYELL1-May-10 19:25
DanYELL1-May-10 19:25 
AnswerRe: Pointer/address to CFormView Pin
PJ Arends1-May-10 20:01
professionalPJ Arends1-May-10 20:01 
GeneralRe: Pointer/address to CFormView Pin
DanYELL2-May-10 5:29
DanYELL2-May-10 5:29 
GeneralRe: Pointer/address to CFormView Pin
Richard MacCutchan2-May-10 6:40
mveRichard MacCutchan2-May-10 6:40 
AnswerRe: Pointer/address to CFormView Pin
Richard MacCutchan1-May-10 21:27
mveRichard MacCutchan1-May-10 21:27 
QuestionMulti Threading for Graphic engines Pin
silversamand1-May-10 18:53
silversamand1-May-10 18:53 
AnswerRe: Multi Threading for Graphic engines Pin
Software_Developer1-May-10 21:02
Software_Developer1-May-10 21:02 
AnswerRe: Multi Threading for Graphic engines Pin
Michel Godfroid2-May-10 7:17
Michel Godfroid2-May-10 7:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.