Thread Event Objects
-
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.
-
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.
For custom messages that are mapped using
ON_MESSAGE
the handler must have a signature that returns anLRESULT
and takes 2 parameter ofWPARAM
andLPARAM
. 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.
-
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.
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
-
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
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.