المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Thread Event Objects



C++ Programming
07-31-2009, 03:00 PM
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.