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. Thread Event Objects

Thread Event Objects

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++
4 Posts 3 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

    Hallo i have the following problem: i need to start and end a thread by using Event Objects in MFC, following is the code in the ThreadView.cpp: Event m_threadStart; Event m_threadEnd; ON_MESSAGE(WM_CREATE, OnCreate) // causes a casting problem void CThreadView::OnStartThread() { m_threadStart.SetEvent (); } void CThreadView::OnStopThread() { m_threadEnd.SetEvent (); } int CThreadView::OnCreate(LPCREATESTRUCT lpCreateStruct) // defined in ThreadView.h liks this: afx_msg OnCreate(LPCREATESTRUCT lpCreateStruct) { HWND hWnd = GetSafeHwnd(); AfxBeginThread(ThreadProc, hWnd, THREAD_PRIORITY_NORMAL); return 0; } LONG CThreadView::OnThreadended(WPARAM wParam, LPARAM lParam) { CString strThreadEnd = _T("Thread ended."); AfxMessageBox(strThreadEnd, 0,0); return 0; } UINT ThreadProc(LPVOID param) { CString strThread_Start = _T("Thread aktiviert."); CString strThread_Stop = _T("Thread deaktiviert."); CString strThread_Caption = _T("Thread"); ::WaitForSingleObject(m_threadStart.m_hObject , INFINITE); ::MessageBox((HWND)param, strThread_Start, strThread_Caption, MB_OK); bool bKeepRunning = true; while(bKeepRunning) { int nResult = ::WaitForSingleObject (m_threadStart.m_hObject ,0); if(nResult == WAIT_OBJECT_0) bKeepRunning = false; } ::PostMessage((HWND)param, WM_THREADENDED, 0, 0); return 0; } First the Compiler bring an error c2440: ON_MESSAGE(WM_CREATE, OnCreate) // causes a casting problem I can not cast. Something is terribly wrong with this code , please help.

    _ G 2 Replies Last reply
    0
    • S susanne1

      Hallo i have the following problem: i need to start and end a thread by using Event Objects in MFC, following is the code in the ThreadView.cpp: Event m_threadStart; Event m_threadEnd; ON_MESSAGE(WM_CREATE, OnCreate) // causes a casting problem void CThreadView::OnStartThread() { m_threadStart.SetEvent (); } void CThreadView::OnStopThread() { m_threadEnd.SetEvent (); } int CThreadView::OnCreate(LPCREATESTRUCT lpCreateStruct) // defined in ThreadView.h liks this: afx_msg OnCreate(LPCREATESTRUCT lpCreateStruct) { HWND hWnd = GetSafeHwnd(); AfxBeginThread(ThreadProc, hWnd, THREAD_PRIORITY_NORMAL); return 0; } LONG CThreadView::OnThreadended(WPARAM wParam, LPARAM lParam) { CString strThreadEnd = _T("Thread ended."); AfxMessageBox(strThreadEnd, 0,0); return 0; } UINT ThreadProc(LPVOID param) { CString strThread_Start = _T("Thread aktiviert."); CString strThread_Stop = _T("Thread deaktiviert."); CString strThread_Caption = _T("Thread"); ::WaitForSingleObject(m_threadStart.m_hObject , INFINITE); ::MessageBox((HWND)param, strThread_Start, strThread_Caption, MB_OK); bool bKeepRunning = true; while(bKeepRunning) { int nResult = ::WaitForSingleObject (m_threadStart.m_hObject ,0); if(nResult == WAIT_OBJECT_0) bKeepRunning = false; } ::PostMessage((HWND)param, WM_THREADENDED, 0, 0); return 0; } First the Compiler bring an error c2440: ON_MESSAGE(WM_CREATE, OnCreate) // causes a casting problem I can not cast. Something is terribly wrong with this code , please help.

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      For custom messages that are mapped using ON_MESSAGE the handler must have a signature that returns an LRESULT and takes 2 parameter of WPARAM and LPARAM. So your OnCreate function must look like this - LRESULT CThreadView::OnCreate(WPARAM wParam, LPARAM lParam);

      «_Superman_» I love work. It gives me something to do between weekends.

      1 Reply Last reply
      0
      • S susanne1

        Hallo i have the following problem: i need to start and end a thread by using Event Objects in MFC, following is the code in the ThreadView.cpp: Event m_threadStart; Event m_threadEnd; ON_MESSAGE(WM_CREATE, OnCreate) // causes a casting problem void CThreadView::OnStartThread() { m_threadStart.SetEvent (); } void CThreadView::OnStopThread() { m_threadEnd.SetEvent (); } int CThreadView::OnCreate(LPCREATESTRUCT lpCreateStruct) // defined in ThreadView.h liks this: afx_msg OnCreate(LPCREATESTRUCT lpCreateStruct) { HWND hWnd = GetSafeHwnd(); AfxBeginThread(ThreadProc, hWnd, THREAD_PRIORITY_NORMAL); return 0; } LONG CThreadView::OnThreadended(WPARAM wParam, LPARAM lParam) { CString strThreadEnd = _T("Thread ended."); AfxMessageBox(strThreadEnd, 0,0); return 0; } UINT ThreadProc(LPVOID param) { CString strThread_Start = _T("Thread aktiviert."); CString strThread_Stop = _T("Thread deaktiviert."); CString strThread_Caption = _T("Thread"); ::WaitForSingleObject(m_threadStart.m_hObject , INFINITE); ::MessageBox((HWND)param, strThread_Start, strThread_Caption, MB_OK); bool bKeepRunning = true; while(bKeepRunning) { int nResult = ::WaitForSingleObject (m_threadStart.m_hObject ,0); if(nResult == WAIT_OBJECT_0) bKeepRunning = false; } ::PostMessage((HWND)param, WM_THREADENDED, 0, 0); return 0; } First the Compiler bring an error c2440: ON_MESSAGE(WM_CREATE, OnCreate) // causes a casting problem I can not cast. Something is terribly wrong with this code , please help.

        G Offline
        G Offline
        Garth J Lancaster
        wrote on last edited by
        #3

        have a look at this http://msdn.microsoft.com/en-us/library/k35k2bfs(VS.80).aspx[^] look carefully at "The type of the function must be afx_msg LRESULT (CWnd::*)(WPARAM, LPARAM).", their example

        // implementation for WM_MYMESSAGE handler
        LRESULT CMyWnd::OnMyMessage( WPARAM wParam, LPARAM lParam )
        {
        // ... Handle message here
        return 0L;
        }

        vs yours ..

        int CThreadView::OnCreate(LPCREATESTRUCT lpCreateStruct)

        interestingly, your "OnThreadended" ie

        LONG CThreadView::OnThreadended(WPARAM wParam, LPARAM lParam)
        {
        CString strThreadEnd = _T("Thread ended.");

        AfxMessageBox(strThreadEnd, 0,0);

        return 0;
        }

        appears ok

        S 1 Reply Last reply
        0
        • G Garth J Lancaster

          have a look at this http://msdn.microsoft.com/en-us/library/k35k2bfs(VS.80).aspx[^] look carefully at "The type of the function must be afx_msg LRESULT (CWnd::*)(WPARAM, LPARAM).", their example

          // implementation for WM_MYMESSAGE handler
          LRESULT CMyWnd::OnMyMessage( WPARAM wParam, LPARAM lParam )
          {
          // ... Handle message here
          return 0L;
          }

          vs yours ..

          int CThreadView::OnCreate(LPCREATESTRUCT lpCreateStruct)

          interestingly, your "OnThreadended" ie

          LONG CThreadView::OnThreadended(WPARAM wParam, LPARAM lParam)
          {
          CString strThreadEnd = _T("Thread ended.");

          AfxMessageBox(strThreadEnd, 0,0);

          return 0;
          }

          appears ok

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

          Thanks for your help. 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.

          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