How to get handle to main frame in CDocument
-
hi, I want to post message from my worker thread to the main frame window in a SDI application. To do so I need to pass hWnd or pMainWnd to the child thread. Because I create my thread in CDocument::OnNewDocument, I need to get handle or pointer to the main frame window from inside CDocument. Is there any way to do that ?
-
hi, I want to post message from my worker thread to the main frame window in a SDI application. To do so I need to pass hWnd or pMainWnd to the child thread. Because I create my thread in CDocument::OnNewDocument, I need to get handle or pointer to the main frame window from inside CDocument. Is there any way to do that ?
-
hi, I want to post message from my worker thread to the main frame window in a SDI application. To do so I need to pass hWnd or pMainWnd to the child thread. Because I create my thread in CDocument::OnNewDocument, I need to get handle or pointer to the main frame window from inside CDocument. Is there any way to do that ?
use AfxGetMainWnd();and typecast to the mainframe pointer. I dont know if it works or not just give a try.if its not working we will try again.
-
thanks, i have tried AfxGetMainWnd() it works fine in CMyDoc but when I pass the pointer to my thread and call postmessage from inside my thread, program crash. the code is like this: CMyDOc::OnNewDocument() { m_pMyWnd = AfxGetMainWnd(); m_pMyWnd->PostMessage(WM_MYMSG,0,0); --> run OK CX25Thread* m_pX25Thread; m_pX25Thread = (CX25Thread*)AfxBeginThread(RUNTIME_CLASS (CX25Thread),THREAD_PRIORITY_NORMAL, 0, // stack size CREATE_SUSPENDED); m_pX25Thread->m_pMyWnd = m_pMyWnd; m_pX25Thread->ResumeThread(); } In Mythread: when I call m_pMyWnd->PostMessage(WM_MYMSG,0,0); --> program crash ! i don't understand why
-
thanks, i have tried AfxGetMainWnd() it works fine in CMyDoc but when I pass the pointer to my thread and call postmessage from inside my thread, program crash. the code is like this: CMyDOc::OnNewDocument() { m_pMyWnd = AfxGetMainWnd(); m_pMyWnd->PostMessage(WM_MYMSG,0,0); --> run OK CX25Thread* m_pX25Thread; m_pX25Thread = (CX25Thread*)AfxBeginThread(RUNTIME_CLASS (CX25Thread),THREAD_PRIORITY_NORMAL, 0, // stack size CREATE_SUSPENDED); m_pX25Thread->m_pMyWnd = m_pMyWnd; m_pX25Thread->ResumeThread(); } In Mythread: when I call m_pMyWnd->PostMessage(WM_MYMSG,0,0); --> program crash ! i don't understand why
I took an old multithreaded app and gutted it to use AfxGetMainWnd() to populate a member variable in the thread and the CMainFrame is receiving the messages without any apparent problems. The message was registered with RegisterWindowMessage(). All the thread creation code is consistent as your snippet so I'm not sure what it is you are fighting. Do you have any other details that might help us identify where our code differs?
-
I took an old multithreaded app and gutted it to use AfxGetMainWnd() to populate a member variable in the thread and the CMainFrame is receiving the messages without any apparent problems. The message was registered with RegisterWindowMessage(). All the thread creation code is consistent as your snippet so I'm not sure what it is you are fighting. Do you have any other details that might help us identify where our code differs?
I've found the problem AfxGetMainWnd() returns 0 in CMyDoc::OnNewDocument() AfxGetMainWnd() only return after all MainFrame, Doc, View have finished initialize. So to get the correct pointer, I must put it earliest in the end of InitInstance and it work.
-
thanks, i have tried AfxGetMainWnd() it works fine in CMyDoc but when I pass the pointer to my thread and call postmessage from inside my thread, program crash. the code is like this: CMyDOc::OnNewDocument() { m_pMyWnd = AfxGetMainWnd(); m_pMyWnd->PostMessage(WM_MYMSG,0,0); --> run OK CX25Thread* m_pX25Thread; m_pX25Thread = (CX25Thread*)AfxBeginThread(RUNTIME_CLASS (CX25Thread),THREAD_PRIORITY_NORMAL, 0, // stack size CREATE_SUSPENDED); m_pX25Thread->m_pMyWnd = m_pMyWnd; m_pX25Thread->ResumeThread(); } In Mythread: when I call m_pMyWnd->PostMessage(WM_MYMSG,0,0); --> program crash ! i don't understand why
Try passing the HWND to the thread instead of the CWnd pointer. You can get the HWND by doing something like
m_pX25Thread->m_hWnd = m_pMyWnd->m_hWnd;
. Best wishes, Hans -
I've found the problem AfxGetMainWnd() returns 0 in CMyDoc::OnNewDocument() AfxGetMainWnd() only return after all MainFrame, Doc, View have finished initialize. So to get the correct pointer, I must put it earliest in the end of InitInstance and it work.