定义两个线程,分别给两个textEdit写入当前时间,可以看到两个线程相互独立
CWinThread *pThread1 , *pThread2;
static UINT Thread_Func1(LPVOID pParam)
{
C线程Dlg * DLG = (C线程Dlg *)pParam;
while (true)
{
CTime time = CTime::GetCurrentTime();
DLG->GetDlgItem(IDC_EDIT1)->SetWindowTextW(time.Format("%Y-%m-%d %H:%M:%S %A"));
::Sleep(1000);
}
}
static UINT Thread_Func2(LPVOID pParam)
{
C线程Dlg * DLG = (C线程Dlg *)pParam;
while (true)
{
::Sleep(500);
CTime time = CTime::GetCurrentTime();
DLG->GetDlgItem(IDC_EDIT2)->SetWindowTextW(time.Format("%Y-%m-%d %H:%M:%S %A"));
::Sleep(500);
}
}
用一个按钮开启两个线程
void C线程Dlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
pThread1 = ::AfxBeginThread(Thread_Func1, this);
pThread2 = ::AfxBeginThread(Thread_Func2, this);
}
两个线程是各自更新时间的