关键代码
#include <time.h>
time_t now_time;
CString str;
struct tm *Ptr;
UINT FlashTime(LPVOID LParam)
{
CTimeTestDlg *pWnd = (CTimeTestDlg *)LParam;
pWnd->doFlashTime();
return -1;
}
void CTimeTestDlg::doFlashTime()
{
while (1)
{
Sleep(1000);
now_time = time(NULL);
Ptr = localtime(&now_time);
str.Format(L"%04d-%02d-%02d %02d:%02d:%02d\r\n", (1900 + Ptr->tm_year), Ptr->tm_mon + 1, Ptr->tm_mday, Ptr->tm_hour, Ptr->tm_min, Ptr->tm_sec);
setTxet(IDC_TIME_SHOW, str);
if (Ptr->tm_min == 40) {
MessageBoxA(0, "计时结束", "计时器", 0);
break;
}
}
}
CWinThread *te3=NULL;
//主动结束线程
//TerminateThread(te3->m_hThread, 1);
void CTimeTestDlg::OnBnClickedBtnStartTimer()
{
te3=AfxBeginThread(FlashTime, (LPVOID)this);
}