UINT ThreadProc(LPVOID param)
-
Can you tell me please how can call or access a global variable or a function defined in th Doc.cpp or anyaother class from inside UINT ThreadProc (LPVOID param)? UINT ThreadProc(LPVOID param) { CTestDoc* pDoc = GetDocument(); // error: GetDocument() is not defined??? ASSERT_VALID(pDoc); ::WaitForSingleObject(m_Thread_Sel_Start.m_hObject , INFINITE); ::MessageBox((HWND)param, strThread_Start, strThread_Caption, MB_OK); CSQLCommand::FindSQLCmd(pDoc,m_strSelect);// error :m_strSelect is defined in the CTestView.h but he can not find it , pDoc is already unkown bool bKeepRunning = true; while(bKeepRunning) { int nResult = ::WaitForSingleObject (m_Thread_Sel_End.m_hObject ,0); if(nResult == WAIT_OBJECT_0) bKeepRunning = false; } ::PostMessage((HWND)param, WM_THREADENDED, 0, 0); return 0; } Can i define the UINT ThreadProc(LPVOID param) in the CTestDoc.cpp? And why when i declare UINT ThreadProc(LPVOID param) in the CTestView.h i get this error: error C3867: "CTestView::ThreadProc": use "&CTestView::ThreadProc". and when i use &CTestView::ThreadProc i get a csting problem that can not be solved through an explict cast.
-
Can you tell me please how can call or access a global variable or a function defined in th Doc.cpp or anyaother class from inside UINT ThreadProc (LPVOID param)? UINT ThreadProc(LPVOID param) { CTestDoc* pDoc = GetDocument(); // error: GetDocument() is not defined??? ASSERT_VALID(pDoc); ::WaitForSingleObject(m_Thread_Sel_Start.m_hObject , INFINITE); ::MessageBox((HWND)param, strThread_Start, strThread_Caption, MB_OK); CSQLCommand::FindSQLCmd(pDoc,m_strSelect);// error :m_strSelect is defined in the CTestView.h but he can not find it , pDoc is already unkown bool bKeepRunning = true; while(bKeepRunning) { int nResult = ::WaitForSingleObject (m_Thread_Sel_End.m_hObject ,0); if(nResult == WAIT_OBJECT_0) bKeepRunning = false; } ::PostMessage((HWND)param, WM_THREADENDED, 0, 0); return 0; } Can i define the UINT ThreadProc(LPVOID param) in the CTestDoc.cpp? And why when i declare UINT ThreadProc(LPVOID param) in the CTestView.h i get this error: error C3867: "CTestView::ThreadProc": use "&CTestView::ThreadProc". and when i use &CTestView::ThreadProc i get a csting problem that can not be solved through an explict cast.
Your problem stems mainly from the fact that the ThreadProc function is static, and therefore cannot access any instance members of your application class. The easiest fix is the following: When you call AfxBeginThread, pass the "this" pointer as your Param. Then, inside the ThreadProc, cast the Param back to whatever object it was when you passed it originally, such as your app class or your document class. That way, you now have access to all the instance members of your class by using the -> operator.
-
Your problem stems mainly from the fact that the ThreadProc function is static, and therefore cannot access any instance members of your application class. The easiest fix is the following: When you call AfxBeginThread, pass the "this" pointer as your Param. Then, inside the ThreadProc, cast the Param back to whatever object it was when you passed it originally, such as your app class or your document class. That way, you now have access to all the instance members of your class by using the -> operator.
Hello, i tried it the way you said, but it did not work , maybe i did something wrong, hier is the code: void CTestView::ThreadStart(CTestDoc* pDoc, CString strSel) { m_Thread_Sel_Start.SetEvent (); HWND hWnd = GetSafeHwnd(); m_strSelect = strSel; m_pDoc = pDoc;// i need to access the CTestDoc.cpp, i passed pDoc from the CTestDoc.cpp to the CTestView.cpp // and then assigend pDoc to m_pDoc which defined in the top of the CTestView.cpp AfxBeginThread(ThreadProc, this/*hWnd*/, THREAD_PRIORITY_NORMAL);// Hier i passed this to the ThreadProc function } UINT ThreadProc ( LPVOID param ) { (CTestView*)param-> // casting to CTestView did not work? return 0; }
-
Hello, i tried it the way you said, but it did not work , maybe i did something wrong, hier is the code: void CTestView::ThreadStart(CTestDoc* pDoc, CString strSel) { m_Thread_Sel_Start.SetEvent (); HWND hWnd = GetSafeHwnd(); m_strSelect = strSel; m_pDoc = pDoc;// i need to access the CTestDoc.cpp, i passed pDoc from the CTestDoc.cpp to the CTestView.cpp // and then assigend pDoc to m_pDoc which defined in the top of the CTestView.cpp AfxBeginThread(ThreadProc, this/*hWnd*/, THREAD_PRIORITY_NORMAL);// Hier i passed this to the ThreadProc function } UINT ThreadProc ( LPVOID param ) { (CTestView*)param-> // casting to CTestView did not work? return 0; }
You're almost there. The correct cast is:
((CTestView*)param)->function();
-
You're almost there. The correct cast is:
((CTestView*)param)->function();
-
okay but i got an application crash hier: #ifdef _DEBUG void CTestView::AssertValid() const { CView::AssertValid(); } #ifndef _WIN32_WCE void CTestView::Dump(CDumpContext& dc) const { CView::Dump(dc); } #endif #endif
Do you have a valid CTestView object before starting your thread? If not, that will cause this crash.