UINT __cdecl MyThreadProc(LPVOID lpParameter) // thread generation function
{
CEvent* pEvent = (CEvent*)(lpParameter);
VERIFY(pEvent != NULL);
// Wait for the event to be signaled
::WaitForSingleObject(pEvent->m_hObject, INFINITE);
// Terminate the thread
::AfxEndThread(0, FALSE);
return 0L;
}
void CModalLessDlg::OnBnClickedButton1() // start the thread
{
// TODO: start
pEvent = new CEvent(FALSE, FALSE);
// Create a thread that will wait on the event
//CWinThread* pThread;
pThread = ::AfxBeginThread(&MyThreadProc, pEvent, 0, 0, CREATE_SUSPENDED, NULL);
pThread->m_bAutoDelete = FALSE;
pThread->ResumeThread();
// show the status of thread
CString st1="Thread started";
SetDlgItemText(IDC_EDIT1, st1);
}
void CModalLessDlg::OnBnClickedButton2() // setevent and terminate the thread
{
pEvent->SetEvent();
// Wait for the thread to consume the event and return
::WaitForSingleObject(pThread->m_hThread, INFINITE);
delete pThread;
delete pEvent;
// show the status of thread
CString st2="Thread Terminated!";
SetDlgItemText(IDC_EDIT1, st2);
}
I love tennis and programming. I am not genius, so I collect the knowledge piece by piece.
Monday, May 27, 2013
kernel event object and thread in MFC
one button to create the kernel event object and worker thread, and the other button to signal the event and terminate the thread.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment