Create a Thread and Use it.
Thread is very important for us to program.
You can take some steps to create it and use it and terminite it.
1:create,MUST create a global ThreadProc that run the things you want.
like:
CTestPint2App theApp;
CString threadRet;
DWORD WINAPI threadProc()
//long __stdcall CAboutDlg::threadProc()
{
CTime time;
CString m_time;
int i=0;
for(;;)
{
time=CTime::GetCurrentTime();
m_time=time.Format("%H:%M:%S");
AfxMessageBox(m_time);
//::SetDlgItemText(AfxGetMainWnd()->m_hWnd,IDC_TIME,m_time);
i++;
Sleep(3000);
if(i=3)
threadRet="3";
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////
// CTestPint2App initialization
2:use it ,must call an api:
//like:
public :
DWORD threadId;
HANDLE hThread;
//above are needed.
void CAboutDlg::OnThreadBegin()
{
hThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)threadProc,NULL,0,&threadId);
}
//3:terminte the thread is simple too.
like:
void CAboutDlg::OnEndThread()
{
TerminateThread(hThread,1);
}
4:but to use it ,you must declear some global var to store the thread's status
and then according the status,you can do your own desition.
//like:
CTestPint2App theApp;
CString threadRet;//must global!.
DWORD WINAPI threadProc()
////
void CAboutDlg::OnUseThread()
{ int end=0;
OnThreadBegin();
while(!end){
if(threadRet.Compare("")!=0){
AfxMessageBox("get threadRet:"+threadRet);
end=1;
OnEndThread();
return;
}
::Sleep(100);
}
}
///ok!enjoy it,please.
You can take some steps to create it and use it and terminite it.
1:create,MUST create a global ThreadProc that run the things you want.
like:
CTestPint2App theApp;
CString threadRet;
DWORD WINAPI threadProc()
//long __stdcall CAboutDlg::threadProc()
{
CTime time;
CString m_time;
int i=0;
for(;;)
{
time=CTime::GetCurrentTime();
m_time=time.Format("%H:%M:%S");
AfxMessageBox(m_time);
//::SetDlgItemText(AfxGetMainWnd()->m_hWnd,IDC_TIME,m_time);
i++;
Sleep(3000);
if(i=3)
threadRet="3";
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////
// CTestPint2App initialization
2:use it ,must call an api:
//like:
public :
DWORD threadId;
HANDLE hThread;
//above are needed.
void CAboutDlg::OnThreadBegin()
{
hThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)threadProc,NULL,0,&threadId);
}
//3:terminte the thread is simple too.
like:
void CAboutDlg::OnEndThread()
{
TerminateThread(hThread,1);
}
4:but to use it ,you must declear some global var to store the thread's status
and then according the status,you can do your own desition.
//like:
CTestPint2App theApp;
CString threadRet;//must global!.
DWORD WINAPI threadProc()
////
void CAboutDlg::OnUseThread()
{ int end=0;
OnThreadBegin();
while(!end){
if(threadRet.Compare("")!=0){
AfxMessageBox("get threadRet:"+threadRet);
end=1;
OnEndThread();
return;
}
::Sleep(100);
}
}
///ok!enjoy it,please.