//时钟回调函数
static void WINAPI CallFUN(LPVOID lpArgToCompletionRoutine,
DWORD dwTimerLowValue,
DWORD dwTimerHighValue)
{
int thrid = ::GetCurrentThreadId();
CMFCTrainDlg* pdlg = reinterpret_cast<CMFCTrainDlg*>(lpArgToCompletionRoutine);
//工作线程无法直接更新界面,需要通过发送消息到ui线程。这里相当于updatedata函数
pdlg->SendMessage(WM_UPDATA, true);
pdlg->m_nEdit++;
pdlg->SendMessage(WM_UPDATA,false);
}
//工作线程创建时钟,并进入循环等待,当时钟触发,执行apc函数后,sleepex返回
static void THRFUN(CMFCTrainDlg* pdlg)
{
int thrid = ::GetCurrentThreadId();
HANDLE htimer = ::CreateWaitableTimer(NULL, true, NULL);
ENSURE(htimer);
LARGE_INTEGER duetime;
duetime.QuadPart = -10000000;
::SetWaitableTimer(htimer, &duetime, 1000, CallFUN, pdlg, true);
while (!pdlg->m_thrflag)
SleepEx(INFINITE, true);
}
void startrun()
{
CWinThread* wthr = ::AfxBeginThread(AFX_THREADPROC(THRFUN), this);
}
//自定义消息WM_UPDATA
ON_MESSAGE(WM_UPDATA, &CMFCTrainDlg::OnUpdateData)
//消息处理函数
afx_msg LRESULT CMFCTrainDlg::OnUpdateData(WPARAM wParam, LPARAM lParam)
{
UpdateData(wParam);
return 0;
}
运行效果会自动更新编辑框数字