Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. UINT ThreadProc(LPVOID param)

UINT ThreadProc(LPVOID param)

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++question
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    susanne1
    wrote on last edited by
    #1

    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.

    Richard Andrew x64R 1 Reply Last reply
    0
    • S susanne1

      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.

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      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.

      S 1 Reply Last reply
      0
      • Richard Andrew x64R Richard Andrew x64

        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.

        S Offline
        S Offline
        susanne1
        wrote on last edited by
        #3

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

        Richard Andrew x64R 1 Reply Last reply
        0
        • S susanne1

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

          Richard Andrew x64R Offline
          Richard Andrew x64R Offline
          Richard Andrew x64
          wrote on last edited by
          #4

          You're almost there. The correct cast is:

          ((CTestView*)param)->function();

          S 1 Reply Last reply
          0
          • Richard Andrew x64R Richard Andrew x64

            You're almost there. The correct cast is:

            ((CTestView*)param)->function();

            S Offline
            S Offline
            susanne1
            wrote on last edited by
            #5

            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

            Richard Andrew x64R 1 Reply Last reply
            0
            • S susanne1

              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

              Richard Andrew x64R Offline
              Richard Andrew x64R Offline
              Richard Andrew x64
              wrote on last edited by
              #6

              Do you have a valid CTestView object before starting your thread? If not, that will cause this crash.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups